国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看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);
  }
 
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 龙珠z国语291集普通话 | 亚洲国产传媒99综合 | 欧美日韩综合精品 | 久久精品国产一区二区三区不卡 | 国产精品高清在线观看 | baoyu123成人免费看视频 | 色淫av| 国产韩国精品一区二区三区 | av小说在线观看 | av片在线观看 | 日韩欧美视频在线 | 精品在线视频一区 | 日本精品视频一区二区 | 午夜爽| 色片在线观看 | 激情综合在线观看 | 久久国产精品久久久久久电车 | 国产精品一区二 | 无码日韩精品一区二区免费 | 成年人在线观看免费视频 | 久久久精品综合 | 性色av一区二区三区红粉影视 | 日韩一区二区影视 | 国产露脸国语对白在线 | 免费在线观看黄色 | av基地网| 午夜爽爽 | 久久精品电影网 | 伊人一区| 毛片特级 | 欧美一级免费高清 | 免费观看av | 精品亚洲成a人在线观看 | 免费看毛片的网站 | 国产日韩欧美高清 | 久久一区 | 国产黄色av | 视频1区2区 | 亚洲成人精品 | 成年人视频在线观看免费 | 黄色av网站免费 |