亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JAVA獲取HTTP請求頭的方法示例

發布時間:2020-09-29 19:26:15 來源:腳本之家 閱讀:487 作者:tterminator 欄目:編程語言

本文實例講述了JAVA獲取HTTP請求頭的方法。分享給大家供大家參考,具體如下:

在利用Java網絡編程時,利用Java獲取HTTP Request 和 Response頭字段;

可以利用Java語言根據需要添加自定義的HTTP頭字段,而不必拘泥于標準HTTP定義的頭字段。

代碼如下:

public class TestURL {
  public static void main(String[] args) {
    String destURLStr= "http://www.baidu.com";
    URL destURL = null;
    URLConnection urlCon = null;
    HttpURLConnection httpUrlCon= null;
    String readResFile = "C:/Users/zhoujw/Desktop/readResFile.html";
    BufferedWriter bw = null;
    try {
      bw = new BufferedWriter(new FileWriter(readResFile));
      destURL = new URL(destURLStr);
      urlCon = destURL.openConnection();
      httpUrlCon = (HttpURLConnection)urlCon;
      //set request property
      httpUrlCon.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
      //可根據需要添加自定義請求頭
      httpUrlCon.setRequestProperty("Test Header1", "test1");
      httpUrlCon.setRequestProperty("Test Header2", "test2");
      httpUrlCon.connect();
      BufferedReader br = new BufferedReader(new InputStreamReader(httpUrlCon.getInputStream(), "gbk"));
      String webpage = null;
      while((( webpage = br.readLine()) != null))
      {
//       System.out.println(webpage);
        bw.write(webpage);
        bw.flush();
      }
      //debug
      System.out.println("Self Define Headers:");
      System.out.println(" Test Header1: " + httpUrlCon.getRequestProperty("Test Header1"));
      System.out.println(" Test Header2: " + httpUrlCon.getRequestProperty("Test Header2"));
      System.out.println();
      //echo request property
      echoRequestHeaders(httpUrlCon);
      //echo response property
      echoResponseHeaders(httpUrlCon);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public static void echoRequestHeaders(HttpURLConnection httpUrlCon){
    System.out.println("Request Headers:");
    System.out.println(" " + httpUrlCon.getRequestMethod() + " / " + " HTTP/1.1");
    System.out.println(" Host: " + httpUrlCon.getRequestProperty("Host"));
    System.out.println(" Connection: " + httpUrlCon.getRequestProperty("Connection"));
    System.out.println(" Accept: " + httpUrlCon.getRequestProperty("Accept"));
    System.out.println(" User-Agent: " + httpUrlCon.getRequestProperty("User-Agent"));
    System.out.println(" Accept-Encoding: " + httpUrlCon.getRequestProperty("Accept-Encoding"));
    System.out.println(" Accept-Language: " + httpUrlCon.getRequestProperty("Accept-Language"));
    System.out.println(" Cookie: " + httpUrlCon.getRequestProperty("Cookie"));
    System.out.println(" Connection: " + httpUrlCon.getHeaderField("Connection"));//利用另一種讀取HTTP頭字段
    System.out.println();
  }
  public static void echoResponseHeaders(HttpURLConnection httpUrlCon) throws IOException{
    System.out.println("Response Headers:");
    System.out.println(" " + "HTTP/1.1 " + httpUrlCon.getResponseCode() + " " + httpUrlCon.getResponseMessage());
    System.out.println(" status: " + httpUrlCon.getResponseCode() + " " + httpUrlCon.getResponseMessage());
    System.out.println(" content-encoding: " + httpUrlCon.getContentEncoding());
    System.out.println(" content-length : " + httpUrlCon.getContentLength());
    System.out.println(" content-type: " + httpUrlCon.getContentType());
    System.out.println(" Date: " + httpUrlCon.getDate());
    System.out.println(" ConnectTimeout: " + httpUrlCon.getConnectTimeout());
    System.out.println(" expires: " + httpUrlCon.getExpiration());
    System.out.println(" content-type: " + httpUrlCon.getHeaderField("content-type"));//利用另一種讀取HTTP頭字段
    System.out.println();
  }
}

運行結果:

Self Define Headers:
 Test Header1: test1
 Test Header2: test2
Request Headers:
 GET / HTTP/1.1
 Host: www.baidu.com
 Connection: keep-alive
 Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
 User-Agent: Java/1.6.0_20
 Accept-Encoding: gzip,deflate,sdch
 Accept-Language: null
 Cookie: null
 Connection: Keep-Alive
http://www.baidu.com
Response Headers:
 HTTP/1.1 200 OK
 status: 200 OK
 content-encoding: gzip
 content-length : -1
 content-type: text/html; charset=utf-8
 Date: 1427817028000
 ConnectTimeout: 0
 expires: 1427817001000
 content-type: text/html; charset=utf-8

更多關于java相關內容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結》、《Java文件與目錄操作技巧匯總》、《Java數據結構與算法教程》、《Java操作DOM節點技巧總結》和《Java緩存操作技巧匯總》

希望本文所述對大家java程序設計有所幫助。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

荔浦县| 北安市| 阜新| 金堂县| 阳东县| 正阳县| 巴楚县| 香格里拉县| 沈阳市| 方正县| 玛沁县| 宿松县| 海伦市| 石首市| 岐山县| 尤溪县| 西安市| 和顺县| 泰来县| 富源县| 深泽县| 天台县| 华蓥市| 军事| 雷波县| 萨嘎县| 大竹县| 宁强县| 崇文区| 洪泽县| 大城县| 彩票| 内江市| 东丰县| 富宁县| 台州市| 保德县| 重庆市| 那曲县| 盘山县| 衡阳县|