Using Java to get HTTP response headers

Source: Internet
Author: User

Introduction

An HTTP response header is a string of information that is sent by a Web server to a client's request, which may contain important information such as the content type of the requested document, the length of the document, the character set that encodes the content, the date time, the expiration time of the content, the last modification time of the content, the server model, and the cache. Understanding this information has important implications for Java's network programming.

HTTP communication mechanism

HTTP uses content types, which are the types of files that the Web server returns to a Web browser. All of these types are modeled on the MIME Internet Mail protocol, where the Web server tells the Web browser what kind of file it has, whether it is an HTML document, a GIF format image, a sound file, or a standalone application. Most Web browsers have a series of configurable helper applications that tell the browser how to handle the various types of content that the Web server sends over. The HTTP communication mechanism is that during a complete HTTP communication, the following 7 steps will be completed between the Web browser and the Web server:

(1)      Establish TCP ConnectionsInbefore HTTP work begins, the Web browser first establishes a connection to the Web server over the network, which is done via TCP, which works with the IP protocol to build the Internet, known as the TCP/IP protocol family, so the internet is also known as tcp/ IP network. HTTP is a higher level of application-level protocol than TCP, according to the rules, only the low-level protocol is established before the connection can be more layer protocol, therefore, the first to establish a TCP connection, the port number of the general TCP connection is (2)      Web browser sends request command to Web serverOnce you have establishedTCP connection, the Web browser sends a request command to the Web serverFor example:get/sample/hello.jsp http/1.1 (3)      Web browser sends request header informationAfter the browser sends its request command, it also takes the form of a header message to theThe Web server sends some other information, and then the browser sends a blank line to notify the server that it has ended sending the header message. (4)      Web Server AnswerAfter the client makes a request to the server, the server responds back to the client, and the first part of the http/1.1 is the protocol version number and the response status code.(5)    The Web server sends the reply header information, just as the client sends information about itself along with the request, and the server sends the user with the answer about its own data and the requested document.(6)    After the Web server sends a data Web server to the browser to send header information to the browser, it sends a blank line to indicate that the header information is sent to the end, and then it sends the actual data requested by the user in the format described in the Content-type reply header information(7)    The Web server shuts down the TCP connection in general, onceThe Web server sends the request data to the browser, it closes the TCP connection, and if the browser or server joins this line of code in its header information

Connection:keep-alive. The TCP connection remains open after it is sent, so the browser can continue to send requests through the same connection. Maintaining a connection saves the time it takes to establish a new connection for each request and also saves network bandwidth.

HTTP1.1 Common Response codes

regardless of the version, the response code from 100 to 199 always represents a corresponding, 200 to 299 total indication of success, 300 to 399 for redirection, 400 to 499 for client errors, and 500 to 599 for server-side errors.

Here are some common status codes and their meanings

Status Code
meaning
Continue
The server is ready to accept, allowing the client to ask the server if it will accept the request before sending a large amount of data in the request
OK
The request succeeds, and if the Get or POST request contains the request data, the head request contains
301 Moved Permanently
The resource has been permanently moved to a new URL
302 Moved temporarily
Resource is temporarily moved to a new URL
304 Not Modified
The client should load the document from the cache
403 Forbidden
The server understands the request but intends to refuse processing
404 Not Found
The server could not find the requested resource
Internal Server Error
Server does not know how to handle
502 Bad Gateway
When the gateway or proxy server attempts to complete the request, it accepts an invalid corresponding
503 Service Unavailable
The server is temporarily unable to process the request, possibly overloading or maintaining the cause

Get HTTP response header information for some websites in bulk Get the corresponding header code:

Package demo0808.demo1;/** * Realistic response header Code */import java.io.ioexception;import java.net.url;import java.net.URLConnection; Import Java.util.arraylist;import java.util.iterator;import Java.util.list;import Java.util.map;import Java.util.map.entry;import Java.util.set;public class Showhttpheaders {//URL to find private arraylist<string> websites;/** * constructor * @param websites site list */public showhttpheaders (arraylist<string> websites) {this.websites= Websites;} /** * Get response header * Print to console */public void Getheaders () {if (websites==null) {System.err.println ("Query URL cannot be empty! "); return;} try {for (String website:websites) {System.out.println ("----------------Web site:" +website+ "http response header---------------"); URL url = new URL (website); URLConnection connection = Url.openconnection (); map<string, list<string>> headerfields = Connection.getheaderfields (); set<entry<string, list<string>>> entryset = Headerfields.entryset (); Iterator<Entry<String, List<string>>> iterator = Entryset.iteraTor (); while (Iterator.hasnext ()) {entry<string, list<string>> next = Iterator.next (); String Key=next.getkey (); list<string> value = Next.getvalue (); if (key==null) System.out.println (value.tostring ()); ElseSystem.out.println (key+ ":" +value.tostring ());} System.out.println ("");}} catch (IOException e) {System.err.println ("Cannot query URL! ");}}}

Test code:


Package demo0808.demo1;/** * Test Code */import java.util.arraylist;public class Test {public static void main (string[] args) { String web1= "http://www.oschina.net/"; String web2= "http://news.baidu.com/"; String web3= "https://linux.cn/"; String web4= "http://www.taobao.com/"; Arraylist<string> websites=new arraylist<string> (); Websites.add (WEB1); Websites.add (WEB2); Websites.add (WEB3); Websites.add (WEB4); Showhttpheaders showhttpheaders = new Showhttpheaders (websites); Showhttpheaders.getheaders ();}}

Test results:


----------------Website: Http://www.oschina.net/HTTP response header---------------[http/1.1 403 Forbidden]date:[sat, Geneva 2015 02:35:08 gmt]content-length:[700]connection:[keep-alive]content-type:[text/html;charset=utf-8]server:[tengine/ 2.1.0]----------------Website: Http://news.baidu.com/HTTP response header---------------[http/1.1 ok]p3p:[cp= "OTI DSP COR IVA Our IND COM "]transfer-encoding:[chunked]vary:[accept-encoding, Accept-encoding]date:[sat, Geneva 02:35:09 GMT] set-cookie:[baiduid=27b1b47c0413e086084f1e668200715a:fg=1; Expires=sun, 07-aug-16 02:35:09 GMT; max-age=31536000; path=/; domain=.baidu.com; Version=1, localgx=%u5e7f%u5dde%7c%35%34%39%36%7c%u5e7f%u5dde%7c%35%34%39%36; Expires=sat, 15-aug-2015 02:35:09 GMT; path=/; Domain=.news.baidu.com]content-type:[text/html]server:[apache]----------------Website: https://linux.cn/ HTTP response Header---------------[http/1.1 ok]strict-transport-security:[max-age=63072000; includesubdomains; preload] Transfer-encoding:[chunked]date:[sat, 02:35:10 gmt]set-cookie:[dX_516D_SID=QADFVF; Expires=sun, 09-aug-2015 02:35:10 GMT; max-age=86400; path=/; domain=.linux.cn; Secure, dx_516d_lastact=1439001310%09index.php%09; Expires=sun, 09-aug-2015 02:35:10 GMT; max-age=86400; path=/; domain=.linux.cn; Secure, DX_516D_SID=QADFVF; Expires=sun, 09-aug-2015 02:35:10 GMT; max-age=86400; path=/; domain=.linux.cn; Secure, dx_516d_lastvisit=1438997710; Expires=mon, 07-sep-2015 02:35:10 GMT; max-age=2592000; path=/; domain=.linux.cn; Secure, Dx_516d_saltkey=vp3c3gik; Expires=mon, 07-sep-2015 02:35:10 GMT; max-age=2592000; path=/; domain=.linux.cn; Secure httponly]connection:[keep-alive]content-type:[text/html; charset=utf-8]server:[nginx]x-content-type-options:[ Nosniff]----------------Website: Http://www.taobao.com/HTTP response header---------------[http/1.1 302 Found]date:[sat, Geneva 2015 02:35:10 GMT]CONTENT-LENGTH:[258]LOCATION:[HTTPS://WWW.TAOBAO.COM/]SET-COOKIE:[THW=CN; path=/; domain=.taobao.com; Expires=sun, 07-aug-16 02:35:10 GMT;] connection:[keep-alive]content-type:[text/hTml]server:[tengine] 



Using Java to get HTTP response headers

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.