SIMONE
BlogJava
首頁
新隨筆
新文章
聯系
聚合
管理
posts - 495,comments - 227,trackbacks - 0
<
2013年11月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(46)
給我留言
查看公開留言
查看私人留言
隨筆分類
(476)
.NET(14)
ActiveX控件(3)
AJAX(14)
android(4)
AXIS(31)
C++(26)
DELL筆記本重裝系統(1)
flash(14)
hadoop(5)
hadoop mahout(3)
hbase(7)
JAVA(153)
JavaScript(55)
JSP(44)
LINUX(17)
mysql(6)
nodejs(3)
oracle(16)
PHP(2)
rmi(2)
Ruby on rails(1)
solr(5)
spark(2)
SQL SERVER(11)
struts(13)
tomcat(8)
vista(3)
收藏(13)
隨筆檔案
(495)
2016年11月 (1)
2016年10月 (3)
2016年8月 (2)
2016年7月 (7)
2016年6月 (5)
2016年5月 (7)
2016年3月 (3)
2016年2月 (1)
2016年1月 (1)
2015年11月 (2)
2015年9月 (3)
2015年8月 (4)
2015年7月 (1)
2015年6月 (2)
2015年5月 (2)
2015年4月 (6)
2015年3月 (2)
2014年12月 (5)
2014年11月 (2)
2014年10月 (1)
2014年9月 (3)
2014年7月 (1)
2014年6月 (1)
2014年5月 (1)
2014年3月 (1)
2013年11月 (3)
2013年9月 (1)
2013年3月 (3)
2013年2月 (6)
2013年1月 (1)
2012年12月 (1)
2012年11月 (8)
2012年10月 (2)
2012年9月 (9)
2012年8月 (5)
2012年7月 (10)
2012年6月 (2)
2012年5月 (1)
2012年4月 (2)
2012年3月 (5)
2012年2月 (4)
2012年1月 (3)
2011年11月 (4)
2011年10月 (16)
2011年9月 (8)
2011年8月 (1)
2011年7月 (6)
2011年6月 (5)
2011年5月 (3)
2011年4月 (12)
2011年3月 (2)
2011年2月 (3)
2010年12月 (4)
2010年11月 (4)
2010年10月 (1)
2010年9月 (3)
2010年8月 (2)
2010年7月 (3)
2010年6月 (1)
2010年5月 (2)
2010年4月 (12)
2010年2月 (2)
2010年1月 (3)
2009年11月 (1)
2009年9月 (2)
2009年8月 (3)
2009年7月 (2)
2009年6月 (8)
2009年5月 (1)
2009年3月 (3)
2009年2月 (4)
2009年1月 (3)
2008年11月 (6)
2008年8月 (2)
2008年7月 (9)
2008年5月 (5)
2008年4月 (4)
2008年3月 (3)
2008年2月 (2)
2008年1月 (5)
2007年12月 (1)
2007年11月 (3)
2007年10月 (2)
2007年9月 (3)
2007年8月 (6)
2007年7月 (3)
2007年6月 (9)
2007年5月 (6)
2007年4月 (2)
2007年3月 (11)
2007年2月 (10)
2007年1月 (6)
2006年12月 (30)
2006年11月 (7)
2006年10月 (11)
2006年9月 (18)
2006年8月 (31)
2006年7月 (8)
2006年6月 (5)
2006年5月 (1)
2006年4月 (19)
2006年3月 (10)
最新隨筆
1.?nodejs將對象轉換成字符串代碼,動態執行字符串代碼,requirejs使用r.js打包時動態生成配置文件
2.?java keytool證書工具使用小結
3.?移動端Web開發調試之Chrome遠程調試(Remote Debugging)
4.?Genymotion 解決虛擬鏡像下載速度特別慢的問題
5.?ubuntu mate 下的sublime text 3調用中文輸入法的修改
6.?Java 和 HTTP 的那些事(二) 使用代理
7.?正則表達式復雜應用
8.?maven 依賴打包插件
9.? 使用embeded tomcat進行嵌入式javaee開發-啟動tomcat
10.?Secure Kafka Java Producer with Kerberos
11.?Kerberos 配置
12.?ubuntu kerberos配置
13.?kerberos安裝配置
14.?Apache Kafka Security 101
15.?MySQL分庫分表的全局唯一ID生成器方案
16.?java修改static final常量值
17.?MYSQL之表分區----按日期分區
18.?Max MQTT connections
19.?HDFS配置Kerberos認證
20.?Spark History Server配置使用
21.?Spark On Yarn中spark.yarn.jar屬性的使用
22.?Benchmarking Apache Kafka: 2 Million Writes Per Second (On Three Cheap Machines)
23.?Kafka 高性能吞吐揭秘
24.?JAVA實現gif圖片縮放與剪切功能
25.?基于Redis實現分布式鎖
26.?Spring 動態注冊類
27.?模塊化利器: 一篇文章掌握RequireJS常用知識
28.?淺談Spark應用程序的性能調優
29.?playframwork dist 打包時將非項目中的文件也打包進去
30.?利用中文數據跑Google開源項目word2vec
搜索
積分與排名
積分 - 1393322
排名 - 16
最新評論
1.?re: 用javascript與java進行RSA加密與解密[未登錄]
謝謝, 正在研究這塊
--付琪
2.?re: 關于Hibernate一對一不能延遲加載的總結
評論內容較長,點擊標題查看
--哈
3.?re: AJAX+jsp無刷新驗證碼實例
達到
--李迪
4.?re: 關于Hibernate一對一不能延遲加載的總結
很6啊
--一個陌生人
5.?re: 字符串相似度算法[未登錄]
注釋的挺不錯,贊一個。也給樓上點個贊:
org.apache.commons.lang.StringUtils.getLevenshteinDistance
中是有實現。
--lili
閱讀排行榜
1.?JQuery-Dialog(彈出窗口,遮蔽窗口)(108484)
2.?JTree用法及JTree使用經驗總結(46246)
3.?tesseract-ocr識別中文掃描圖片實例講解 (46160)
4.?window.close關閉窗口,不彈出系統提示,直接關閉(43573)
5.?CKeditor 配置使用(42018)
評論排行榜
1.?window.close關閉窗口,不彈出系統提示,直接關閉(18)
2.?tesseract-ocr識別中文掃描圖片實例講解 (15)
3.?CKeditor 配置使用(10)
4.?JQuery-Dialog(彈出窗口,遮蔽窗口)(10)
5.?我寫的javascript常用靜態方法類,分享大家(8)
Http學習之使用HttpURLConnection發送post和get請求
http://blog.csdn.net/totogogo/article/details/1831588
http://blog.csdn.net/pandazxx/article/details/1657109
http://blog.csdn.net/pandazxx/article/details/1660008
最常用的Http請求無非是get和post,get請求可以獲取靜態頁面,也可以把參數放在URL字串后面,傳遞給servlet,post與get的不同之處在于post的參數不是放在URL字串里面,而是放在http請求的正文內。
在Java中可以使用HttpURLConnection發起這兩種請求,了解此類,對于了解soap,和編寫servlet的自動測試代碼都有很大的幫助。
下面的代碼簡單描述了如何使用HttpURLConnection發起這兩種請求,以及傳遞參數的方法:
public
class
HttpInvoker
{
public
static
final
String GET_URL
=
"
http://localhost:8080/welcome1
"
;
public
static
final
String POST_URL
=
"
http://localhost:8080/welcome1
"
;
public
static
void
readContentFromGet()
throws
IOException
{
//
拼湊get請求的URL字串,使用URLEncoder.encode對特殊和不可見字符進行編碼
String getURL
=
GET_URL
+
"
?username=
"
+
URLEncoder.encode(
"
fat man
"
,
"
utf-8
"
);
URL getUrl
=
new
URL(getURL);
//
根據拼湊的URL,打開連接,URL.openConnection函數會根據URL的類型,
//
返回不同的URLConnection子類的對象,這里URL是一個http,因此實際返回的是HttpURLConnection
HttpURLConnection connection
=
(HttpURLConnection) getUrl
.openConnection();
//
進行連接,但是實際上get request要在下一句的connection.getInputStream()函數中才會真正發到
//
服務器
connection.connect();
//
取得輸入流,并使用Reader讀取
BufferedReader reader
=
new
BufferedReader(
new
InputStreamReader(
connection.getInputStream()));
System.out.println(
"
=============================
"
);
System.out.println(
"
Contents of get request
"
);
System.out.println(
"
=============================
"
);
String lines;
while
((lines
=
reader.readLine())
!=
null
)
{
System.out.println(lines);
}
reader.close();
//
斷開連接
connection.disconnect();
System.out.println(
"
=============================
"
);
System.out.println(
"
Contents of get request ends
"
);
System.out.println(
"
=============================
"
);
}
public
static
void
readContentFromPost()
throws
IOException
{
//
Post請求的url,與get不同的是不需要帶參數
URL postUrl
=
new
URL(POST_URL);
//
打開連接
HttpURLConnection connection
=
(HttpURLConnection) postUrl
.openConnection();
//
Output to the connection. Default is
//
false, set to true because post
//
method must write something to the
//
connection
//
設置是否向connection輸出,因為這個是post請求,參數要放在
//
http正文內,因此需要設為true
connection.setDoOutput(
true
);
//
Read from the connection. Default is true.
connection.setDoInput(
true
);
//
Set the post method. Default is GET
connection.setRequestMethod(
"
POST
"
);
//
Post cannot use caches
//
Post 請求不能使用緩存
connection.setUseCaches(
false
);
//
This method takes effects to
//
every instances of this class.
//
URLConnection.setFollowRedirects是static函數,作用于所有的URLConnection對象。
//
connection.setFollowRedirects(true);
//
This methods only
//
takes effacts to this
//
instance.
//
URLConnection.setInstanceFollowRedirects是成員函數,僅作用于當前函數
connection.setInstanceFollowRedirects(
true
);
//
Set the content type to urlencoded,
//
because we will write
//
some URL-encoded content to the
//
connection. Settings above must be set before connect!
//
配置本次連接的Content-type,配置為application/x-www-form-urlencoded的
//
意思是正文是urlencoded編碼過的form參數,下面我們可以看到我們對正文內容使用URLEncoder.encode
//
進行編碼
connection.setRequestProperty(
"
Content-Type
"
,
"
application/x-www-form-urlencoded
"
);
//
連接,從postUrl.openConnection()至此的配置必須要在connect之前完成,
//
要注意的是connection.getOutputStream會隱含的進行connect。
connection.connect();
DataOutputStream out
=
new
DataOutputStream(connection
.getOutputStream());
//
The URL-encoded contend
//
正文,正文內容其實跟get的URL中'?'后的參數字符串一致
String content
=
"
firstname=
"
+
URLEncoder.encode(
"
一個大肥人
"
,
"
utf-8
"
);
//
DataOutputStream.writeBytes將字符串中的16位的unicode字符以8位的字符形式寫道流里面
out.writeBytes(content);
out.flush();
out.close();
//
flush and close
BufferedReader reader
=
new
BufferedReader(
new
InputStreamReader(
connection.getInputStream()));
String line;
System.out.println(
"
=============================
"
);
System.out.println(
"
Contents of post request
"
);
System.out.println(
"
=============================
"
);
while
((line
=
reader.readLine())
!=
null
)
{
System.out.println(line);
}
System.out.println(
"
=============================
"
);
System.out.println(
"
Contents of post request ends
"
);
System.out.println(
"
=============================
"
);
reader.close();
connection.disconnect();
}
/**
*
@param
args
*/
public
static
void
main(String[] args)
{
//
TODO Auto-generated method stub
try
{
readContentFromGet();
readContentFromPost();
}
catch
(IOException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上面的
readContentFromGet()函數產生了一個get請求,傳給servlet一個username參數,值為"fat man"。
readContentFromPost()函數產生了一個post請求,傳給servlet一個firstname參數,值為"一個大肥人"。
HttpURLConnection.connect函數,實際上只是建立了一個與服務器的tcp連接,并沒有實際發送http請求。無論是post還是get,http請求實際上直到
HttpURLConnection
.getInputStream()這個函數里面才正式發送出去。
在
readContentFromPost() 中,順序是重中之重,對connection對象的一切配置(那一堆set函數)都必須要在connect()函數執行之前完成。而對 outputStream的寫操作,又必須要在inputStream的讀操作之前。這些順序實際上是由http請求的格式決定的。
http 請求實際上由兩部分組成,一個是http頭,所有關于此次http請求的配置都在http頭里面定義,一個是正文content,在connect()函 數里面,會根據HttpURLConnection對象的配置值生成http頭,因此在調用connect函數之前,就必須把所有的配置準備好。
緊接著http頭的是http請求的正文,正文的內容通過outputStream寫入,實際上outputStream不是一個網絡流,充其量是個字符串流,往里面寫入的東西不會立即發送到網絡,而是在流關閉后,根據輸入的內容生成http正文。
至 此,http請求的東西已經準備就緒。在getInputStream()函數調用的時候,就會把準備好的http請求正式發送到服務器了,然后返回一個 輸入流,用于讀取服務器對于此次http請求的返回信息。由于http請求在getInputStream的時候已經發送出去了(包括http頭和正 文),因此在getInputStream()函數之后對connection對象進行設置(對http頭的信息進行修改)或者寫入 outputStream(對正文進行修改)都是沒有意義的了,執行這些操作會導致異常的發生。
posted on 2013-11-14 17:03
SIMONE
閱讀(441)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Copyright ©2025 SIMONE Powered By
博客園
模板提供:
滬江博客
主站蜘蛛池模板:
亚洲成熟xxxxx电影
|
亚洲AV无码久久精品色欲
|
亚洲专区在线视频
|
中文字幕日本人妻久久久免费
|
亚洲精品国产福利一二区
|
亚洲av午夜国产精品无码中文字
|
亚洲精品无码成人片久久不卡
|
国产亚洲高清在线精品不卡
|
亚洲免费综合色在线视频
|
亚洲国产品综合人成综合网站
|
亚欧人成精品免费观看
|
亚洲视频中文字幕在线
|
日韩a毛片免费观看
|
亚洲福利视频一区二区
|
亚欧乱色国产精品免费视频
|
亚洲精品专区在线观看
|
国产高潮流白浆喷水免费A片
|
久久久久亚洲AV成人网
|
亚洲18在线天美
|
午夜精品在线免费观看
|
亚洲国产精品无码久久久秋霞2
|
免费观看91视频
|
亚洲男人天堂影院
|
成人毛片18女人毛片免费视频未
|
免费高清资源黄网站在线观看
|
免费国产美女爽到喷出水来视频
|
亚洲精品国产综合久久久久紧
|
国产精品深夜福利免费观看
|
亚洲视频在线免费
|
激情内射亚洲一区二区三区
|
一个人看www在线高清免费看
|
国产精品久久永久免费
|
亚洲精品久久无码av片俺去也
|
亚洲国产日韩成人综合天堂
|
在线人成免费视频69国产
|
亚洲视频在线观看不卡
|
国产精品美女自在线观看免费
|
中文字幕在线视频免费观看
|
日本xxwwxxww在线视频免费
|
国产免费内射又粗又爽密桃视频
|
久久精品无码一区二区三区免费
|