Java进阶(三十一) Web服务调用
前言
有朋友问了一个问题:如何调用已知的音乐服务接口,服务文档如下:
https://www.evernote.com/shard/s744/sh/c37cd503-68fc-4406-b8f2-5e90095be303/19b67e36aa2ccd19
查看代码之后,按照以往的服务调用方法实现,结果无法实现。很是费解,求教大师兄之后,问题,迎刃而解,只能说自己需要学习的地方还有很多。
完整代码如下:
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(); } public static String getByHttpPost() throws Exception { // 1.创建HttpClient对象。(创建默认的httpClient实例.) CloseableHttpClient httpclient = HttpClients.createDefault(); // 首页 String indexStr = "http://music-download.dev.fotoable.net/musics/lists"; // 搜索 String searchStr = "http://music-download.dev.fotoable.net/public/search"; // 2.创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。(创建httppost) HttpPost httppost = new HttpPost(searchStr); List<NameValuePair> formparams = new ArrayList<NameValuePair>(); // 3.如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数; // 对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。 // 关键词搜索传参(创建参数队列) 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.调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个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. 释放连接。无论执行方法是否成功,都必须释放连接 httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }
参考文献
http://blog.****.net/wangpeng047/article/details/19624529
感触
自己还是应该多多学习才是,遇到了问题不要紧,要懂得回过头来认真总结,学习。这样才能不断进步。