激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

香港云服务器
服務器之家 - 編程語言 - JAVA教程 - 簡單介紹Java網絡編程中的HTTP請求

簡單介紹Java網絡編程中的HTTP請求

2020-01-06 14:20goldensun JAVA教程

這篇文章主要介紹了簡單介紹Java網絡編程中的HTTP請求,需要的朋友可以參考下

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);
  }
 
}

延伸 · 閱讀

精彩推薦
619
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 精品中文字幕在线播放 | 亚洲小视频在线 | a级在线| 未成年人在线观看 | 一级做a爱片久久毛片a高清 | 久久精品中文字幕一区二区三区 | 中文字幕在线永久 | 国产在线一区二区三区 | 成人午夜免费福利 | 久久国产精品久久久久 | 综合图区亚洲 | 一区二区三区四区免费 | 欧美精品电影一区二区 | 国产一区二区视频在线播放 | 婷婷久久综合九色综合色多多蜜臀 | 午夜影视一区二区 | 99久久精品免费 | 色人久久 | 蜜桃一本色道久久综合亚洲精品冫 | 一级大片久久 | 中国一级毛片在线播放 | 国产精品手机在线亚洲 | 国产羞羞视频在线免费观看 | 免费在线观看成年人视频 | 午夜精品一区二区三区免费 | 色妞视频男女视频 | 一区二区免费看 | 久久久久久麻豆 | 亚洲天堂字幕 | 成人午夜视频在线观看免费 | av国语| 92精品国产自产在线 | 李宗瑞国产福利视频一区 | 免费国产成人高清在线看软件 | 一区二区三区无码高清视频 | 狠狠操夜夜爱 | 午夜精品小视频 | av影院在线播放 | 视频一区国产 | 国产视频精品在线 | 秋霞a级毛片在线看 |