一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久亚洲伊人 | 欧洲美女女同 | 亚洲va欧美va国产综合久久 | 成 人免费va视频 | 热99re久久精品国产 | 久久水蜜桃亚洲AV无码精品偷窥 | 国产精品国语自产拍在线观看 | 艹的好爽 | 欧美特黄视频在线观看 | 久久精品国产色蜜蜜麻豆国语版 | 玩两个少妇女邻居 | 色悠久久久久综合欧美99 | 国产精品久久免费 | 岛国在线播放v片免费 | 小小水蜜桃视频高清在线观看免费 | 女仆色永久免费网站 | 国产成人久久精品区一区二区 | 亚洲 欧美 国产 综合久久 | 无限在线观看视频大全免费高清 | 能播放18xxx18女同 | gogort人体的最新网站 | 狠狠色| 韩国丽卡三级作品 | tubehdxx丝袜正片 | 久久综合给会久久狠狠狠 | 精品视频 久久久 | 日韩夫妻性生活 | 国产精品一区二区在线观看完整版 | 国产一卡2卡3卡四卡精品网站 | 午夜国产在线观看 | 色天天综合色天天看 | 国产第一草草影院 | 好男人资源免费观看 | 精品视频国产 | 免费观看在线 | 99热精品成人免费观看 | 欧美另类z0zxi| 91国内在线国内在线播放 | 扒开黑女人p大荫蒂老女人 扒开大腿狠狠挺进视频 | 日本阿v精品视频在线观看 日本xxx片免费高清在线 | 幻女free性zoz0交 |