JAVA如何取得HTTP要求標頭

來源:互聯網
上載者:User

標籤: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 propertyhttpUrlCon.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();}//debugSystem.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 propertyechoRequestHeaders(httpUrlCon);//echo response propertyechoResponseHeaders(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: test2Request 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-Alivehttp://www.baidu.comResponse 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如何取得HTTP要求標頭

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.