zoukankan      html  css  js  c++  java
  • 调用webservice查询手机号归属地信息

    导读:Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。在这里我们使用soap协议往webservice发送信息。

    \" data-mce-src=

    package cn.mzba.service; 

    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;

    import org.xmlpull.v1.XmlPullParser;

    import android.util.Xml;

    public class MobileService {

    public static String findAddress(String mobile)throws Exception{
    InputStream is = MobileService.class.getClassLoader().getResourceAsStream("mobilesoap.xml");
    byte[] data = StreamTool.readStream(is);
    String xml = new String(data,"UTF-8");
    String soap = xml.replaceAll("\\$mobile", mobile);
    byte[] result = soap.getBytes("UTF-8");
    String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setConnectTimeout(5 * 1000);

    conn.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
    conn.setRequestProperty("Content-Length", String.valueOf(result.length));
    OutputStream os = conn.getOutputStream();
    os.write(result);
    os.flush();
    os.close();

    InputStream isSocp = conn.getInputStream();
    return parse(isSocp);
    }

    public static String parse(InputStream is)throws Exception{
    XmlPullParser parser= Xml.newPullParser();
    parser.setInput(is,"UTF-8");
    int event = parser.getEventType();
    while(event != XmlPullParser.END_DOCUMENT){
    switch (event) {
    case XmlPullParser.START_TAG:
    if("getMobileCodeInfoResult".equals(parser.getName())){
    return parser.nextText();
    }
    break;
    }
    event = parser.next();
    }
    return null;
    }
    }


    转自:http://www.eoeandroid.com/code/2012/0128/673.html

     
  • 相关阅读:
    ajax异步上传图片&SpringMVC后台代码
    Jdk与Tomcat配置与安装
    Jpa常用注解@Test
    关于ssh和ajax小小总结
    关于EL表达式的大小写问题。谁来帮我解答?
    关于spring管理hibernate事物
    关于session更新的问题
    ssh使用ajax异步通讯. json与对象转换的几个小问题
    通过http Post XML文件的接口 (System.Web.IHttpHandler)
    HTTP Post方法
  • 原文地址:https://www.cnblogs.com/shanzei/p/2414129.html
Copyright © 2011-2022 走看看