HTTP network request considerations for base Get,post requests

Source: Internet
Author: User
Tags finally block

The more common requests in HTTP network requests are get and post requests, and others such as head, put, and custom options requests are ignored first The request object used in Java is typically httpurlconnection the address we request requires that the URL object be used to encapsulate the string address as a URL connurl = new URL ("http://www.xxxx./") The steps of the network request GET RequestThe first thing to do is to encode the requested parameter in Chinese Java.net.URLEncoder.encode (parameters.get (name), "UTF-8") otherwise the server receives    To the Chinese data will become garbled, this is a human error.    Stitching encoded parameters and URLs to form a real request address String URL = url + "?" + params 1, create URL object url connurl = new URL ("http://www.xxxx./") 2. Open the URL link to get httpurlconnection httpurlconnection httpconn = (httpurlconnection) connurl. Openco Nnection ();
     3, set the properties of the connection, ----request header information         The information that mimics the browser's request header          connection, Accept, user-agent, cookies, etc. request header header information         httpconn.setrequestproperty ("Accept", "*/*");    4, establish connection          Httpconn.connect ();    5, get information about the head of the response  , here is mainly to obtain information about the cookie, so that the next visit no longer need to log in, directly through the cookie to obtain data (of course, with the major websites deliberately defense , only the cookie is obtained, the next time the cookie is carried, it may be intercepted, i.e. it cannot be crawled to the desired data)     httpconn.getheaderfield ("KeyName"); If you want to traverse all the information in the response's response header, the code is as follows  
// Response Header Acquisition            map<string, list<string>> headers = httpconn.getheaderfields ();                        // iterate through all the response header fields             for (String key:headers.keySet ()) {                                System.out.println (key+ ":  " +Httpconn.getheaderfield (key) );            }

This is the response header information returned by the analog access www.baidu.com

6. Read the contents of the response, the Web page HTML file, or the specific return value data
//define the BufferedReader input stream to read the response of the URL and set the encoding methodBufferedReader in=NULL; Inch=NewBufferedReader (NewInputStreamReader (httpconn. getInputStream (),"UTF-8"));//the Universal encoding format is Utf-8String Line; //read the returned content             while(line = In.readline ())! =NULL) {result+=Line ; } httpconn.disconnect ();//Finally, you need to close the Httpconn connection
There is also a need to close the character input stream BufferedReader This needs to be closed in the finally block
finally {    try  {         ifnull) {            in.close ();         }      Catch (IOException ex) {           ex.printstacktrace ();      }}

The above is a normal process of GET request, of course, there are many ways to read content, some can be stored directly in the computer files, these methods are not considered

A few things to note about a GET request:

    • Request parameters, Chinese, other escape symbols need to be encoded
The following code, the Chinese and special meaning characters will be encoded, and the English alphabet does not need to encode
String url = "http://www.baidu.com";        String ss= "Are you kidding Me";         Try {            System.out.println (java.net.URLEncoder.encode (URL,"UTF-8"));            System.out.println (Java.net.URLEncoder.encode (ss,"UTF-8"));         Catch (unsupportedencodingexception e) {            e.printstacktrace ();        }

Output Result:

  

    • Get request header preferably set up a proxy, or it may be blocked by the site, denied access

, if this is set to Firefox, simulate the browser's request header can be

Httpconn.setrequestproperty ("User-agent",                    "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 6.1) ");
    • Make sure to turn off the input and output stream when the connection is disconnected, and reduce the abnormal appearance.

such as the HttpURLConnection connection above, there are also Bufferreader objects

A complete GET Request code

     Public StaticString sendget (string url, linkedhashmap<string, string>parameters) {String result= "";//The returned resultBufferedReader in =NULL;//Read response input streamStringBuffer SB =NewStringBuffer ();//Storage ParametersString params = "";//the parameters after encoding        Try {            //Encoding Request Parameters            if(parameters.size () = = 1) {                 for(String name:parameters.keySet ()) {sb.append (name). Append ("="). Append (Java.net.URLEncoder.encode (parameters.get (name), "UTF-8")); } params=sb.tostring (); } Else {                 for(String name:parameters.keySet ()) {sb.append (name). Append ("="). Append (Java.net.URLEncoder.encode (parameters.get (name), "UTF-8")). Append ("&"); } String temp_params=sb.tostring (); Params= temp_params.substring (0, Temp_params.length ()-1); } String Full_url= URL + "?" +params; //Create a URL objectURL Connurl =NewURL (Full_url); //Open URL ConnectionHttpURLConnection Httpconn =(HttpURLConnection) connurl. OpenConnection (); //Setting Common PropertiesHttpconn.setrequestproperty ("Accept", "*/*"); Httpconn.setrequestproperty ("Connection", "keep-alive"); Httpconn.setrequestproperty ("User-agent",                    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1) "); //establish an actual connectionHttpconn.connect (); //Response Header Acquisitionmap<string, list<string>> headers =Httpconn.getheaderfields (); //iterate through all the response header fields             for(String key:headers.keySet ()) {//System.out.println (key+ ":" +httpconn.getheaderfield (key));            }            //define the BufferedReader input stream to read the response of the URL and set the encoding methodin =NewBufferedReader (NewInputStreamReader (httpconn. getInputStream (),"GBK"));            String Line; //read the returned content             while(line = In.readline ())! =NULL) {result+=Line ; }        } Catch(Exception e) {e.printstacktrace (); System.out.println ("Internal problem with HTTP request Method"); } finally {            Try {                if(In! =NULL) {in.close (); }            } Catch(IOException ex) {ex.printstacktrace (); }        }        returnresult; }
View Code

Post Network request modeThe POST request is basically the same way as a GET request, the difference is that the request data is passed differently, there is no limit to the size of the data that can be submitted by post.The GET request theoretically has no size limit, because the browser restricts its length, so there is a limit to the size of the pass dataPOST request does not need to set the user-agent of the request castIf you want to avoid landing, you will need to log in the request to return the cookie information in the Httpurlconnect to set the request to cast information in thesuch as Connection.setrequestproperty ("Cookie", "Place cookie Data");
Connection.setrequestproperty ("Content-type",                    "Application/x-www-form-urlencoded;charset=utf-8");
The main way to set the server to parse the data, the server will be the body of the data in the key value of the keys to the method of parsing,we only need to use Request.getparameter ("KeyName") in the background to read the data.other formats for submitting data:
    • Multipart/form-data type is mainly used when uploading files;
    • Application/x-www-form-urlencoded type is mainly used when committing k-v, of course, this method can also be set JSON in V to submit JSON data;
    • Application/json type is mainly used to pass JSON data, the level of deep data;
 Public Staticstring Sendpost (String curl, string param) {string result= "";//The returned resultBufferedReader in =NULL;//Read response input stream        Try {            //Create a connectionURL url =NewURL (Curl); HttpURLConnection Connection=(httpurlconnection) URL. OpenConnection (); Connection.setdooutput (true); Connection.setdoinput (true); Connection.setrequestmethod ("POST"); Connection.setusecaches (false); Connection.setinstancefollowredirects (true); Connection.setrequestproperty ("Content-type",                    "Application/x-www-form-urlencoded;charset=utf-8");            Connection.connect (); //POST RequestBufferedWriter out =NewBufferedWriter (NewOutputStreamWriter (Connection.getoutputstream (), "UTF-8"));            Out.write (param);            Out.flush ();            Out.close (); //Read Response//define the BufferedReader input stream to read the response of the URL and set the encoding methodin =NewBufferedReader (NewInputStreamReader (Connection.getinputstream (), "UTF-8"));            String Line; //read the returned content             while(line = In.readline ())! =NULL) {result+=Line ; }        } Catch(Exception e) {e.printstacktrace (); System.out.println ("Internal problem with HTTP request Method"); } finally {            Try {                if(In! =NULL) {in.close (); }            } Catch(IOException ex) {ex.printstacktrace (); }        }        returnresult; }

Post request different ways of submitting data have corresponding parsing method, JSON parsing and file upload next time write a topic

HTTP network request considerations for base Get,post requests

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.