How Java obtains HTTP request headers

Source: Internet
Author: User

Use Java to get HTTP Request and response header fields while using Java network programming;


You can use the Java language to add custom HTTP header fields as needed without having to stick to the header fields of standard HTTP definitions.


The code is as follows:

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));d Esturl = new URL (DESTURLSTR); Urlcon = Desturl.openconnection (); Httpurlcon = (httpurlconnection) Urlcon;//set request Propertyhttpurlcon.setrequestproperty ("accept-encoding", "GZIP,DEFLATE,SDCH");// You can add a custom request header Httpurlcon.setrequestproperty ("Test Header1", "test1") as needed, 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"));//Use another read HTTP header field 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 "));//Use another read HTTP header field System.out.println ();}} 
Operation Result:

Self Define headers:test header1:test1 Test header2:test2request headers:get/  http/1.1 Host:www.baidu.com Connec Tion: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 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



How Java obtains HTTP request headers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.