HttpClient framework
HTTP protocol may be the most widely used and important protocol on the Internet. More and more Java applications need to access network resources directly through HTTP protocol. Although the basic function of accessing HTTP protocol has been provided in the java net package of JDK, for most applications, the functions provided by JDK library itself are not rich and flexible enough. HttpClient is a sub project under Apache Jakarta Common, which is used to provide an efficient, up-to-date and feature rich client programming toolkit supporting HTTP protocol, and it supports the latest version and suggestions of HTTP protocol. HttpClient has been used in many projects, such as the other two famous open source projects Cactus and HTMLUnit on Apache Jakarta
HttpClient dependency introduction
In POM Introducing HttpClient dependency into XML
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
HttpPost application
Simulate the java class that implements the post request under the http protocol. The characteristics of post submission are as follows:
- Submit the request to the server in post mode
- Parameters are encapsulated in the request body and submitted
- There is no limit to the length of the data
/** *POST request with parameter format of json */ @Test public void testPost(){ //Fill in the interface address String url = ""; //Specify interface request mode HttpPost post = new HttpPost(url); //Prepare test data Map<String,String> parameter = new HashMap<>(); parameter.put("phone","13777777777"); parameter.put("pwd","123456"); //Format parameters StringEntity entity = new StringEntity(JSON.toJSONString(parameter), "UTF-8"); post.setEntity(entity); //Create client and initiate request CloseableHttpClient client = HttpClientBuilder.create().build(); CloseableHttpResponse response = client.execute(post); //Convert the response message into Map set JSONObject res = new JSONObject(EntityUtils.toString(response.getEntity(), "UTF-8")); res.toMap(); //Print response message System.out.print("Response status code:" + response.getStatusLine().getStatusCode()); System.out.print("Response message:" + response.getEntity); } /** *POST request with parameter format of form */ @Test public void test(){ String url = ""; HttpPost post = new HttpPost(url); String phone = ""; String pwd = ""; List<BasicNameValuePair> parameter = new ArrayList<BasicNameValuePair>(); parameter.add(new BasicNameValuePair("phone",phone)); parameter.add(new BasicNameValuePair("pwd",pwd)); post.setEntity(new UrlEncodeFormEntity(parameter,"UTF-8")); CloseableHttpClient client = HttpClientBuilder.create().build(); CloseableHttpResponse response = client.execute(post); JSONObject res = new JSONObject(EntityUtils.toString(response.getEntity(), "UTF-8")); res.toMap(); System.out.print("Response status code:" + response.getStatusLine().getStatusCode()); System.out.print("Response message:" + response.getEntity); }
Processing JSON data also requires the introduction of related dependencies
<!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160810</version> </dependency>
HttpGet application
Simulate the java class that implements the get request under the http protocol, and the characteristics of get submission
-
The get parameter is not encapsulated in the request body, but is directly spliced after the URL with? Form of
-
The data length was originally unlimited, but major browsers and servers limited the URL length.
-
IE limits url length to 2083 (2K+53);
The URL length of firefox (firefox browser) is limited to 65 536 characters, but in fact, the maximum length of a valid URL is not less than 100000 characters;
The url length limit of chrome (Google) exceeds 8182 characters, and 414 errors are returned;
Safari's url length is limited to at least 80000 characters.
The url length of Opera browser is limited to 190000 characters. It can still be edited normally when 190000 characters are entered in the address bar of Opera 9.
public static Map<String, Object> getRequest(String url, Map<String, String> parameter) throws Exception{ Map<String, Object> reslut = new HashMap<>(); //Judge whether there are parameters. If not, directly request the URL if (parameter == null){ HttpGet get = new HttpGet(url); CloseableHttpClient client = HttpClientBuilder.create().build(); response = client.execute(get); JSONObject res = new JSONObject(EntityUtils.toString(response.getEntity(), "UTF-8")); res.toMap(); System.out.print("Response status code:" + response.getStatusLine().getStatusCode()); System.out.print("Response message:" + response.getEntity); } //Convert the Map set into a string in the form of name = value & name = value and splice it after the URL StringBuilder paraStr = new StringBuilder("?"); Set<String> keys = parameter.keySet(); for (String key : keys) { paraStr.append(key).append("=").append(parameter.get(key)).append("&"); } HttpGet get = new HttpGet(url + paraStr.toString()); CloseableHttpClient client = HttpClientBuilder.create().build(); CloseableHttpResponse response = client.execute(get); JSONObject res = new JSONObject(EntityUtils.toString(response.getEntity(), "UTF-8")); res.toMap(); System.out.print("Response status code:" + response.getStatusLine().getStatusCode()); System.out.print("Response message:" + response.getEntity); }
- Get response message from response object: getEntity()
- Get the response status code from the response object: getstatusline() getStatusCode()
- Get the response header information from the response object: getAllHeaders()