Java Advanced (31) Web service invocation

Source: Internet
Author: User

Java Advanced (31) Web service invocation Preface

A friend asked a question: How to invoke a known music service interface, the service documentation is as follows:

Https://www.evernote.com/shard/s744/sh/c37cd503-68fc-4406-b8f2-5e90095be303/19b67e36aa2ccd19

After viewing the code, the results are not implemented as a result of previous service invocation methods. is very puzzling, ask big bro after, problem, solve, can only say oneself need to study place still have many.

The complete code is as follows:

Package plan.http.util; Import Java.io.ioexception;import Java.io.unsupportedencodingexception;import Java.util.arraylist;import Java.util.List; Import Org.apache.http.httpentity;import Org.apache.http.namevaluepair;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.entity.urlencodedformentity;import Org.apache.http.client.methods.closeablehttpresponse;import Org.apache.http.client.methods.httppost;import Org.apache.http.impl.client.closeablehttpclient;import Org.apache.http.impl.client.httpclients;import Org.apache.http.message.basicnamevaluepair;import org.apache.http.util.EntityUtils; public class Httpclientutil {public static void main (string[] args) throws Exception {Httpclientutil.getbyhttppost ();} PU Blic static String Getbyhttppost () throws Exception {//1. Create a HttpClient object. (Create the default HttpClient instance.) Closeablehttpclient httpclient = Httpclients.createdefault ();//home string indexstr = "http:// Music-download.dev.fotoable.net/musics/lists ";//Search for string searchSTR = "Http://music-download.dev.fotoable.net/public/search";//2. Create an instance of the request method and specify the request URL. Create a HttpGet object if you need to send a GET request, or create a HttpPost object if you need to send a POST request. (Create HttpPost) HttpPost HttpPost = new HttpPost (SEARCHSTR); list<namevaluepair> formparams = new arraylist<namevaluepair> ();//3. If you need to send request parameters, you can call HttpGet, HttpPost Common SetParams (Hetpparams params) method to add request parameters;//For HttpPost objects, you can also call the setentity (httpentity entity) method to set the request parameters. Keyword search parameters (create parameter queue) Formparams.add (new Basicnamevaluepair ("keyword", "See You Again"));//Formparams.add (new Basicnamevaluepair ("pwd", "123")); Urlencodedformentity uefentity;try {uefentity = new urlencodedformentity (formparams, "UTF-8"); Httppost.setheader (" User-agent "," mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) gecko/20100101 firefox/41.0 ") httppost.setheader (" Connection "," keep-alive "); Httppost.setheader (" Accept-language "," zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3 "); Httppost.setheader (" Accept-encoding "," gzip, deflate "); Httppost.setheader ("Accept", "TEXT/HTML,APPLICATION/XHTML+XML,APPLICATION/XMl;q=0.9,*/*;q=0.8 "), Httppost.setheader (" Jsessionid "," 536020f424630f22db1f4eaead8e83bd "); Httppost.setEntity ( uefentity); SYSTEM.OUT.PRINTLN ("Executing request" + Httppost.geturi ());//4. Call the Execute (httpurirequest request) of the HttpClient object to send the request , the method returns a HttpResponse. Closeablehttpresponse response = Httpclient.execute (HttpPost); try {httpentity entity = response.getentity (); if (entity ! = null) {System.out.println ("--------------------------------------"); System.out.println ("Response content:" + entityutils.tostring (Entity, "UTF-8")); System.out.println ("--------------------------------------");}} finally {response.close ();}} catch (Clientprotocolexception e) {e.printstacktrace ();} catch (Unsupportedencodingexception E1) {E1.printstacktrace ( );} catch (IOException e) {e.printstacktrace ();} finally {try {//5. Release the connection. The connection httpclient.close () must be released regardless of the success of the execution method; catch (IOException e) {e.printstacktrace ();}} return null;}}

Reference Documents

http://blog.csdn.net/wangpeng047/article/details/19624529

Feelings

Oneself or should be more study is, encountered the problem does not matter, to know back to seriously summarize, study. So as to keep improving.

American and American pictures


Java Advanced (31) Web service invocation

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.