系统之家 - 系统光盘下载网站!

当前位置:系统之家 > 系统教程 > 通过HttpClient请求Web Service的操作

通过HttpClient请求Web Service的操作方法

时间:2017-01-23 17:01:28 作者:chunhua 来源:系统之家 1. 扫描二维码随时看资讯 2. 请使用手机浏览器访问: https://m.xitongzhijia.net/xtjc/20170123/91366.html 手机查看 评论

  Web Service是一个基于可编程的Web应用程序,由于服务端是用Web Service开发的,如果android要调用Web Service服务来获取数据,就需要通过HttpClient发送post请求来获取,操作方法如下。

  方法/步骤:

  注:本文使用的是查询手机号码归属地的Web。

  1、service:https://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

  查询的主要方法:https://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo

通过HttpClient请求Web Service的操作方法

  2、可以看出该方法的两个传入参数的名称;

通过HttpClient请求Web Service的操作方法

  3、下面直接上代码。

  1. 01[java] view plaincopy
  2. 02private void getMobileCodeInfo(){
  3. 03try {
  4. 04final String SERVER_URL = "https://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"; // 定义需要获取的内容来源地址
  5. 05HttpPost request = new HttpPost(SERVER_URL);
  6. 06List<BasicNameValuePair> params = new ArrayList();
  7. 07params.add(new BasicNameValuePair("mobileCode", "136370628")); //(注意这里的号码必须大于6位)
  8. 08params.add(new BasicNameValuePair("userId", ""));
  9. 09request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
  10. 10HttpResponse httpResponse = new DefaultHttpClient().execute(request);
  11. 11if (httpResponse.getStatusLine().getStatusCode() != 404)
  12. 12{
  13. 13String result = EntityUtils.toString(httpResponse.getEntity());
  14. 14System.out.println(result);
  15. 15}
  16. 16} catch (Exception e) {
  17. 17Log.e("eee", ""+e);
  18. 18e.printStackTrace();
  19. 19}
  20. 20}
复制代码
[java] view plaincopy private void getMobileCodeInfo(){ try { final String SERVER_URL = "https://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"; // 定义需要获取的内容来源地址 HttpPost request = new HttpPost(SERVER_URL); List<BasicNameValuePair> params = new ArrayList(); params.add(new BasicNameValuePair("mobileCode", "136370628")); //(注意这里的号码必须大于6位) params.add(new BasicNameValuePair("userId", "")); request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse httpResponse = new DefaultHttpClient().execute(request); if (httpResponse.getStatusLine().getStatusCode() != 404) { String result = EntityUtils.toString(httpResponse.getEntity()); System.out.println(result); } } catch (Exception e) { Log.e("eee", ""+e); e.printStackTrace(); } }

通过HttpClient请求Web Service的操作方法

  通过HttpClient请求Web Service的操作方法就给大家分享到这边了,不会操作的朋友,可以参考教程操作哦!

发表评论

0

没有更多评论了

评论就这些咯,让大家也知道你的独特见解

立即评论

以上留言仅代表用户个人观点,不代表系统之家立场

其他版本软件

人气教程排行

相关系统推荐

官方交流群 软件收录