HTTP請求的細節——請求行
請求行中的GET稱之為請求方式,請求方式有:POST、GET、HEAD、OPTIONS、DELETE、TRACE、PUT,常用的有: GET、 POST
用戶如果沒有設置,默認情況下瀏覽器向服務器發送的都是get請求,例如在瀏覽器直接輸地址訪問,點超鏈接訪問等都是get,用戶如想把請求方式改為post,可通過更改表單的提交方式實現。
不管POST或GET,都用于向服務器請求某個WEB資源,這兩種方式的區別主要表現在數據傳遞上:如果請求方式為GET方式,則可以在請求的URL地址后以?的形式帶上交給服務器的數據,多個數據之間以&進行分隔,例如:GET /mail/1.html?name=abc&password=xyz HTTP/1.1
GET方式的特點:在URL地址后附帶的參數是有限制的,其數據容量通常不能超過1K。
如果請求方式為POST方式,則可以在請求的實體內容中向服務器發送數據,Post方式的特點:傳送的數據量無限制。
HTTP請求的細節——消息頭
HTTP請求中的常用消息頭
accept:瀏覽器通過這個頭告訴服務器,它所支持的數據類型
Accept-Charset: 瀏覽器通過這個頭告訴服務器,它支持哪種字符集
Accept-Encoding:瀏覽器通過這個頭告訴服務器,支持的壓縮格式
Accept-Language:瀏覽器通過這個頭告訴服務器,它的語言環境
Host:瀏覽器通過這個頭告訴服務器,想訪問哪臺主機
If-Modified-Since: 瀏覽器通過這個頭告訴服務器,緩存數據的時間
Referer:瀏覽器通過這個頭告訴服務器,客戶機是哪個頁面來的 防盜鏈
Connection:瀏覽器通過這個頭告訴服務器,請求完后是斷開鏈接還是何持鏈接
例:
http_get
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class Http_Get { private static String URL_PATH = "http://192.168.1.125:8080/myhttp/pro1.png" ; public Http_Get() { // TODO Auto-generated constructor stub } public static void saveImageToDisk() { InputStream inputStream = getInputStream(); byte [] data = new byte [ 1024 ]; int len = 0 ; FileOutputStream fileOutputStream = null ; try { fileOutputStream = new FileOutputStream( "C:\\test.png" ); while ((len = inputStream.read(data)) != - 1 ) { fileOutputStream.write(data, 0 , len); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (inputStream != null ) { try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (fileOutputStream != null ) { try { fileOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * 獲得服務器端的數據,以InputStream形式返回 * @return */ public static InputStream getInputStream() { InputStream inputStream = null ; HttpURLConnection httpURLConnection = null ; try { URL url = new URL(URL_PATH); if (url != null ) { httpURLConnection = (HttpURLConnection) url.openConnection(); // 設置連接網絡的超時時間 httpURLConnection.setConnectTimeout( 3000 ); httpURLConnection.setDoInput( true ); // 表示設置本次http請求使用GET方式請求 httpURLConnection.setRequestMethod( "GET" ); int responseCode = httpURLConnection.getResponseCode(); if (responseCode == 200 ) { // 從服務器獲得一個輸入流 inputStream = httpURLConnection.getInputStream(); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return inputStream; } public static void main(String[] args) { // 從服務器獲得圖片保存到本地 saveImageToDisk(); } } |
Http_Post
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; public class Http_Post { // 請求服務器端的url private static String PATH = "http://192.168.1.125:8080/myhttp/servlet/LoginAction" ; private static URL url; public Http_Post() { // TODO Auto-generated constructor stub } static { try { url = new URL(PATH); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param params * 填寫的url的參數 * @param encode * 字節編碼 * @return */ public static String sendPostMessage(Map<String, String> params, String encode) { // 作為StringBuffer初始化的字符串 StringBuffer buffer = new StringBuffer(); try { if (params != null && !params.isEmpty()) { for (Map.Entry<String, String> entry : params.entrySet()) { // 完成轉碼操作 buffer.append(entry.getKey()).append( "=" ).append( URLEncoder.encode(entry.getValue(), encode)) .append( "&" ); } buffer.deleteCharAt(buffer.length() - 1 ); } // System.out.println(buffer.toString()); // 刪除掉最有一個& System.out.println( "-->>" +buffer.toString()); HttpURLConnection urlConnection = (HttpURLConnection) url .openConnection(); urlConnection.setConnectTimeout( 3000 ); urlConnection.setRequestMethod( "POST" ); urlConnection.setDoInput( true ); // 表示從服務器獲取數據 urlConnection.setDoOutput( true ); // 表示向服務器寫數據 // 獲得上傳信息的字節大小以及長度 byte [] mydata = buffer.toString().getBytes(); // 表示設置請求體的類型是文本類型 urlConnection.setRequestProperty( "Content-Type" , "application/x-www-form-urlencoded" ); urlConnection.setRequestProperty( "Content-Length" , String.valueOf(mydata.length)); // 獲得輸出流,向服務器輸出數據 OutputStream outputStream = urlConnection.getOutputStream(); outputStream.write(mydata, 0 ,mydata.length); outputStream.close(); // 獲得服務器響應的結果和狀態碼 int responseCode = urlConnection.getResponseCode(); if (responseCode == 200 ) { return changeInputStream(urlConnection.getInputStream(), encode); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "" ; } /** * 將一個輸入流轉換成指定編碼的字符串 * * @param inputStream * @param encode * @return */ private static String changeInputStream(InputStream inputStream, String encode) { // TODO Auto-generated method stub ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte [] data = new byte [ 1024 ]; int len = 0 ; String result = "" ; if (inputStream != null ) { try { while ((len = inputStream.read(data)) != - 1 ) { outputStream.write(data, 0 , len); } result = new String(outputStream.toByteArray(), encode); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Map<String, String> params = new HashMap<String, String>(); params.put( "username" , "admin" ); params.put( "password" , "123" ); String result = Http_Post.sendPostMessage(params, "utf-8" ); System.out.println( "--result->>" + result); } } |