JAVA獲取HTTP請求頭的方法示例
本文實例講述了JAVA獲取HTTP請求頭的方法。分享給大家供大家參考,具體如下:
在利用Java網(wǎng)絡編程時,利用Java獲取HTTP Request 和 Response頭字段;
可以利用Java語言根據(jù)需要添加自定義的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"); //可根據(jù)需要添加自定義請求頭 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(); } }
運行結(jié)果:
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
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結(jié)》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
SpringBoot自定義動態(tài)數(shù)據(jù)源的流程步驟
動態(tài)數(shù)據(jù)源,本質(zhì)上是把多個數(shù)據(jù)源存儲在一個?Map?中,當需要使用某一個數(shù)據(jù)源時,使用?key?獲取指定數(shù)據(jù)源進行處理,本文將給大家介紹一下SpringBoot自定義動態(tài)數(shù)據(jù)源的流程步驟,需要的朋友可以參考下2024-06-06使Java的JButton文字隱藏功能的實現(xiàn)(不隱藏按鈕的前提)
這篇文章主要介紹了使Java的JButton文字隱藏功能的實現(xiàn)(不隱藏按鈕的前提),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01struts2.3.24+spring4.1.6+hibernate4.3.11+mysql5.5.25開發(fā)環(huán)境搭建圖文
這篇文章主要介紹了struts2.3.24+spring4.1.6+hibernate4.3.11+mysql5.5.25開發(fā)環(huán)境搭建圖文教程,感興趣的小伙伴們可以參考一下2016-06-06Java操作MongoDB插入數(shù)據(jù)進行模糊查詢與in查詢功能
今天小編就為大家分享一篇關(guān)于Java操作MongoDB插入數(shù)據(jù)進行模糊查詢與in查詢功能,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12