JAVA获取HTTP请求头的方法示例
本文实例讲述了JAVA获取HTTP请求头的方法。分享给大家供大家参考,具体如下:
在利用Java网络编程时,利用Java获取HTTPRequest和Response头字段;
可以利用Java语言根据需要添加自定义的HTTP头字段,而不必拘泥于标准HTTP定义的头字段。
代码如下:
publicclassTestURL{ publicstaticvoidmain(String[]args){ StringdestURLStr="http://www.baidu.com"; URLdestURL=null; URLConnectionurlCon=null; HttpURLConnectionhttpUrlCon=null; StringreadResFile="C:/Users/zhoujw/Desktop/readResFile.html"; BufferedWriterbw=null; try{ bw=newBufferedWriter(newFileWriter(readResFile)); destURL=newURL(destURLStr); urlCon=destURL.openConnection(); httpUrlCon=(HttpURLConnection)urlCon; //setrequestproperty httpUrlCon.setRequestProperty("Accept-Encoding","gzip,deflate,sdch"); //可根据需要添加自定义请求头 httpUrlCon.setRequestProperty("TestHeader1","test1"); httpUrlCon.setRequestProperty("TestHeader2","test2"); httpUrlCon.connect(); BufferedReaderbr=newBufferedReader(newInputStreamReader(httpUrlCon.getInputStream(),"gbk")); Stringwebpage=null; while(((webpage=br.readLine())!=null)) { //System.out.println(webpage); bw.write(webpage); bw.flush(); } //debug System.out.println("SelfDefineHeaders:"); System.out.println("TestHeader1:"+httpUrlCon.getRequestProperty("TestHeader1")); System.out.println("TestHeader2:"+httpUrlCon.getRequestProperty("TestHeader2")); System.out.println(); //echorequestproperty echoRequestHeaders(httpUrlCon); //echoresponseproperty echoResponseHeaders(httpUrlCon); }catch(MalformedURLExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } } publicstaticvoidechoRequestHeaders(HttpURLConnectionhttpUrlCon){ System.out.println("RequestHeaders:"); 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(); } publicstaticvoidechoResponseHeaders(HttpURLConnectionhttpUrlCon)throwsIOException{ System.out.println("ResponseHeaders:"); 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(); } }
运行结果:
SelfDefineHeaders: TestHeader1:test1 TestHeader2:test2 RequestHeaders: 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 ResponseHeaders: HTTP/1.1200OK status:200OK 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相关内容感兴趣的读者可查看本站专题:《JavaSocket编程技巧总结》、《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。