Differences between HttpURLConnection and HttpClient in Java Network Connection and javaurlconnection
HTTP protocol may be the most widely used and important protocol on the Internet. More and more Java applications need to directly access network resources through HTTP protocol. The SDK's java.net package provides the basic HTTP access function: HttpURLConnection.
HttpURLConnection is a standard java class. HttpURLConnection inherits from URLConnection and can be used to send GET and POST requests to a specified website. Based on URLConnection, it provides the following convenient methods:
- Int getResponseCode (): Get the response code of the server.
- String getResponseMessage (): Get the Server Response Message.
- String getResponseMethod (): Obtain the method for sending the request.
- Void setRequestMethod (String method): Set the method for sending the request.
In general, if you only need a simple page of the Web site to submit a request and obtain the server response, HttpURLConnection is fully qualified. However, in most cases, Web pages on the Web site may not be as simple as they are accessible through a simple URL, you may need to log on and have the required permissions to access this page. In this case, Session and Cookie processing are required. If you plan to use HttpURLConnection to handle these details, it is also possible to implement it, but it is difficult to process it.
To better handle requests to Web sites, including Session, Cookie, and other details, the Apache open-source organization provides an HttpClient project. You can see the name of the project, it is a simple HTTP client (not a browser) that can be used to send HTTP requests and receive HTTP responses. However, the server's response will not be cached, the Javascript code embedded in the HTML page cannot be executed, and the page content will not be parsed or processed.
In short, HttpClient is an enhanced version of HttpURLConnection. HttpURLConnection can do all the things HttpClient can do. HttpURLConnection does not provide some functions, and HttpClient also provides, however, it only focuses on how to send requests, receive responses, and manage HTTP connections. It is easy to use HttpClient to send requests and receive responses. You only need to take the following steps. In addition,
AndroidHttpClient has been successfully integrated, which means that developers can directly use Httpclient in Android applications to access and submit requests and receive responses. For example, an Android app needs to send a request to a specified page, but this page is not a simple page. This page can be accessed only when the user has logged on and the user name is valid. If you use HttpURLConnection to access the protected page, the details to be processed are too complicated. In fact, it is very easy to use a browser to access protected pages in Web applications. users log on to the system through the login page provided by the system, and the browser will be responsible for maintaining the Sesion between the server and the server, if the user name and password meet the requirements, you can access protected resources. In an Android Application, you can use HttpClient to log on to the system. As long as the application uses the same HttpClient to send requests, HttpClient automatically maintains the Session status between the application and the server, that is to say, after the program logs on to the system using HttpClient for the first time, it can access the protected page with HttpClient.