HttpClient Quick Start

Source: Internet
Author: User
Tags session id

1, what is it?

is used to simplify the various communication programming between HTTP client and server.

2. What can be done

Simply put: With a GET or POST request, after encapsulating the URI in the specified format, the request (execute) is executed by httpclient and a response is obtained after it is sent to the server. The server's response information exists in response. In response, get the part of the result you want (such as: header information, response entity information, status line information)

2.1. Read Web page (HTTP/HTTPS) content

Here is a simple example that we have given to access a page (with a POST request)

Closeablehttpclient httpclient = Httpclients.createdefault ();
HttpPost HttpPost = new HttpPost (destination URL of the server);
Httppost.setentity (new stringentity (request parameter);

Response is the response information returned by the server after the POST request is executed
Closeablehttpresponse response = Httpclient.execute (HttpPost);
try {
httpentity entity = response.getentity ();

InputStream in= entity.getcontent ();

BufferedReader br = new BufferedReader (new InputStreamReader (in, "UTF-8"));
String Line;

StringBuffer buf= new StringBuffer ();
While (null! = (line = Br.readline ())) {
Buf.append (line);
}


Get Response header information
System.out.println (Response.getallheaders ());

Response entity
System.out.println (Response.getentity ());

Response status Line
System.out.println (Response.getstatusline ());
} finally {
Response.close ();
}
}


In this example, you first create an instance of the HTTP client (HttpClient), and then select the method to commit is get or post, and finally execute the commit on the HttpClient instance, and finally read the results of the server feedback from the selected submission method. This is the basic process of using httpclient. In fact, with a line of code can also handle the entire request process, very simple!

2.2impersonate the user name and password to log in

The content of many websites is only visible to registered users, in this case you must require the correct user name and password to log in successfully before you can browse to the desired page. Because the HTTP protocol is stateless, that is, the validity of the connection is limited to the current request, and the connection is closed after the request has finished. In this case, the cookie mechanism must be used in order to save the user's login information. In Jsp/servlet, for example, when a browser requests a JSP or a servlet page, the application server returns a parameter called Jsessionid (which varies depending on the application server), and the value is a long, unique string of cookies. This string value is the session ID that is currently accessing the site. The browser takes the cookie information such as Jsessionid on every other page of the site, and the application server obtains the corresponding session information based on reading the session ID.

For Web sites that require users to log on, the user profile is generally saved in the session of the server after the user has successfully logged in, so that when accessing other pages, the application server reads the session ID of the current request according to the cookie sent by the browser to obtain the corresponding session information. Then you can determine whether the user profile exists in the session information, if there is allowed to access the page, otherwise jump to the login page requires the user to enter the account number and password to sign in. This is generally used in the JSP development site in the processing of user login more common method.

In this way, for HTTP clients, if you want to access a protected page, you must simulate the work done by the browser, the first is to request the login page, and then read the cookie value, and then request the login page again to join each parameter required by the login page, and finally request the final page. Of course, in addition to the first request, other requests need to be accompanied by cookie information so that the server can determine whether the current request has been verified. Say so much, but if you use httpclient, you don't even need to add a line of code, you just have to pass the login information to perform the login process, and then directly access the desired page, and access to a normal page without any difference, Because the class httpclient has done all the work for you, it's great! The following example implements the process of such an access.


2.3 Upload Download file






HttpClient Quick Start

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.