In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Brief introduction of HttpClient I and HttpClient
HttpClient is a subproject under Apache Jakarta Common that can be used to provide efficient, up-to-date, feature-rich client programming toolkits that support the HTTP protocol, and it supports the latest versions and recommendations of the HTTP protocol.
HTTP protocol is probably the most widely used and important protocol on Internet, and more and more Java applications need to access network resources directly through HTTP protocol. Although the basic functionality of accessing the HTTP protocol is provided in JDK's java net package, the functionality provided by the JDK library itself is not rich and flexible enough for most applications. HttpClient is a subproject under Apache Jakarta Common to provide efficient, up-to-date, feature-rich client programming toolkits that support the HTTP protocol, and it supports the latest versions and recommendations of the HTTP protocol. HttpClient has been used in many projects, such as two other well-known open source projects on Apache Jakarta, Cactus and HTMLUnit, which use HttpClient.
II. Introduction of FastJson1 and FastJson
Fastjson is a Java library that converts Java objects to JSON format, as well as JSON strings to Java objects.
Fastjson can manipulate any Java object, even pre-existing objects without source code.
Fastjson source code address: https://github.com/alibaba/fastjson
Fastjson Chinese Wiki: https://github.com/alibaba/fastjson/wiki/Quick-Start-CN
FastJson characteristics
Provide server-side and Android client-side parsing tools with good performance.
The toJSONString () and parseObject () methods are provided to convert Java objects to and from JSON. The toJSONString square method is called to convert the object to a JSON string, and the parseObject method in turn converts the JSON string to an object.
Allows you to convert pre-existing objects that cannot be modified (only class, no source code).
Broad support for Java generics.
Allows custom representation of objects and custom serialization classes.
Support for arbitrary complex objects (with deep inheritance hierarchies and widely used generic types).
Examples
1. GetData.java: use httpclient to receive data, and then use fastJson to parse json data into JavaBean.
Package com.get_data.get;import java.io.IOException;import com.alibaba.fastjson.JSON;import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils Public class GetData {public static void main (String [] args) {HttpClient client = new DefaultHttpClient (); / / create a get request instance / / HttpGet get = new HttpGet ("http://www.baidu.com"); HttpGet get = new HttpGet (" http://localhost:7333/a"); System.out.println ("requested uri is:" + get.getURI () Try {/ / client executes get request and returns response entity HttpResponse response = client.execute (get); / / get request status line System.out.println ("request status behavior:" + response.getStatusLine ()); / / get all request headers Header [] headers=response.getAllHeaders () For (Header header: headers) {/ / traverses to get the names and values of all request headers System.out.println (header.getName () + ": -:" + header.getValue ()) } System.out.println ("- -"); / / the entity that gets the response HttpEntity entity = response.getEntity (); if (entityresponse null) {String str=EntityUtils.toString (entity, "UTF-8") System.out.println ("entity:" + str); System.out.println ("the json obtained is:" + str); User newUser = JSON.parseObject (str, User.class); System.out.println ("User:" + newUser); System.out.println ("newUser.getAddress ():" + newUser.getAddress ()) / / System.out.println (EntityUtils.toString (entity, "UTF-8")); System.out.println ("= ="); System.out.println ("content length is:" + entity.getContentLength ());}} catch (ClientProtocolException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace () } finally {/ / release connection client.getConnectionManager (). Shutdown ();}
2. Json data returned by http://localhost:7333/a
{"name": "James", "age": 18, "address": "chongqing"}
3. User.java entity class
Public class User {private String name; private int age; private String sex; public String getSex () {return sex;} public void setSex (String sex) {this.sex = sex;} private String address; public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age } public void setAge (int age) {this.age = age;} public String getAddress () {return address;} public void setAddress (String address) {this.address = address } @ Override public String toString () {return "User {" + "name='" + name +'\'+ ", age=" + age + ", address='" + address +'\'+'}';}}
4 、
Output result:
The requested uri is: http://localhost:7333/a14:31:04.341 [main] DEBUG org.apache.http.impl.conn.BasicClientConnectionManager-Get connection for route {}-> http://localhost:733314:31:04.387 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnectionOperator-Connecting to localhost:733314:31:04.403 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies-CookieSpec selected: default14:31:04.403 [main] DEBUG org.apache.http. Client.protocol.RequestAuthCache-Auth cache not set in the context14:31:04.403 [main] DEBUG org.apache.http.client.protocol.RequestTargetAuthentication-Target auth state: UNCHALLENGED14:31:04.403 [main] DEBUG org.apache.http.client.protocol.RequestProxyAuthentication-Proxy auth state: UNCHALLENGED14:31:04.403 [main] DEBUG org.apache.http.impl.client.DefaultHttpClient-Attempt 1 to execute request14:31:04.403 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnection-Sending request: GET / a HTTP/1.114:31:04.403 [main] DEBUG org.apache.http.wire-> > "GET / a HTTP/1.1 [\ r] [\ n]" 14DEBUG org.apache.http.wire 31HTTP/1.114:31:04.403 04.403 [main] DEBUG org.apache.http.wire-> > "Host: localhost:7333 [\ r] [\ n]" 14HTTP/1.114:31:04.403 31vis04.403 [main] DEBUG org.apache.http.wire-> > "Connection: Keep-Alive [\ r] [\ n]" 14:31 : 04.403 [main] DEBUG org.apache.http.wire-> > "User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_161) [\ r] [\ n]" 14 Apache-HttpClient/4.5.2 31Apache-HttpClient/4.5.2 04.403 [main] DEBUG org.apache.http.wire-> > "[\ r] [\ n]" 14Vera 31vis04.403 [main] DEBUG org.apache.http.headers-> > GET / a HTTP/1.114:31:04.403 [main] DEBUG org.apache.http.headers-> > Host: localhost:733314:31:04.403 [main] DEBUG org.apache.http.headers-> > Connection: Keep-Alive14:31:04.403 [main] DEBUG org.apache.http.headers-> User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_161) 14 purge 31purge 04.419 [main] DEBUG org.apache.http.wire-
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.