Jersey Client API Basics
You first need to create an instance of the Com.sun.jersey. Api.client.Client class.
Import Com.sun.jersey. api.client.Client;
Client client = Client.create ();
or ClientConfig ClientConfig = Clienconfig.defaultclientconfig ();
Client client = Client.create (ClientConfig); The client class is the primary configuration point for creating a RESTful Web Service client. You can use it to configure different client properties and features, and to indicate which resource provider to use. Creating an instance of a client class is a relatively expensive operation, so try to avoid creating unwanted client instances. The better way is to reuse the existing instances as much as possible.
Once you have created an instance of a Client class, you can start using it. In any case, before making a request, you need to create a Web Resource object to encapsulate the Web resources required by the client.
The WEB resource creates a WebResponse object:
Import Com.sun.jersey. Api.client.WebResource;
WebResource WebResource = C.resource ("Https://api.weixin.qq.com/cgi-bin/message/custom/send");
You create a request to send to a Web resource by using the WebResource object, and you handle the response that is returned from the Web resource. For example, you can use the WebResource object to send HTTP GET, PUT, POST, and DELETE requests.
2.1 GET Request: use the Get () method of the WebResource class to submit an HTTP GET request to the WEB resource:
String s = webresource.get (String.class);
This means that if the URL of the WebResource object is Https://api.weixin.qq.com/cgi-bin/message/custom/send, an HTTP GET request will be sent to the address https:// Api.weixin.qq.com/cgi-bin/message/custom/send's resources.
String s = webresource.get (String.class);
You can also specify the query parameters when a get () request is requested. For example, the following code specifies two query parameters in a get () Request:
There are two ways, in fact, it is the same:
The first type:
Multivaluedmap queryparams = new Multivaluedmapimpl ();
Queryparams.add ("param1", val1);
Queryparams.add ("param2", val2);
String s = webresouce.queryparams (Queryparams). get (String.class);
The second type:
Webresource.queryparam ("param1", Val1). Queryparam ("param2", Val2). Get (String.class);
You can also specify the MIME type that the response can accept. For example, the following code specifies that the MIME type of the response can only be text:
String s = webresource.accept ("Text/plain"). Get (String.class);
2.2 Clientresponse
You can also get the HTTP status code for the corresponding request, such as the following example to get the text entity and status code returned by a request:
Clientresponse response = webresource.accept ("Text/plain"). Get (Clientresponse.class);
int status = Response.getstatus ();
String textentity = response.getentity (String.class);
The Clientresponse object represents an HTTP response received by a client.
2.3 Put request: do not want to write
2.4 Post request: A POST request is a combination of a GET request and a PUT request, which means that you can use a POST request to send an entity to the specified WEB resource and receive another entity. Use the Post () method of the WebResource class to send an HTTP POST request to the specified Web resource. The following example shows a POST request that sends a form data with query parameters and URL encoding:
Example 1.
Multivaluedmap formData = new Multivaluedmapimpl ();
Formdata.add ("Name1", "val1");
Formdata.add ("Name2", "val2");
Clientresponse response = Webresource.type ("application/x-www-form-urlencoded"). Post (Clientresponse.class, FormData);
2.5 Delete Request: use the Delete () method of the Web Resource class to send an HTTP delete request to the specified Web resource. For example, the following example shows the deletion of a URI as a https://api.weixin.qq.com/cgi-bin/message/custom/send resource:
Clientresponse response = Webresource.path ("user/123")
. Delete (Clientresponse.class);
Note: theWebresource.path () method can be used in all HTTP requests, which allows you to specify an additional path for the WEB resource to be requested. Another method header () of the Webresouce class can add HTTP header information to your request. If the form is submitted, a new form is required as a parameter submission.
Code example sends a message
/**
* Send information to designated users
* @param message
* @param touser
* @return
*/
Public Wechatmessageresult Send (String message, string touser) {
ClientConfig config = new Defaultclientconfig ();
Client client = client.create (config);
WebResource WebResource = Client.resource ("Https://api.weixin.qq.com/cgi-bin/message/custom/send");
Clientresponse Clientresponse = WebResource
. Queryparam ("Access_token", Getaccesstoken (). GetToken ())
. Post (Clientresponse.class, New Gson (). ToJson (New Massagebuilder ("text"). Touser (Touser). Textcontent (Message). Bulid ()));
if (Clientresponse.getstatus ()! = 200) {
throw new IllegalStateException ("Status error:" + clientresponse.getstatus ());
} else {
return new Gson (). Fromjson (Clientresponse.getentity (String.class), wechatmessageresult.class);
}
}