KimhillZhang  

一个需求需要获取手机归属地,在网上看了好多代码试了好多都不行,都是到conn.setrequestmethod("post")就卡住了,也没有去研究具体是什么问题,后来看了获取手机归属地的webservice,地址:

http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

就自己写了,直接使用调用webservice的方法去实现,结果是可以行的;还显得更简单,,但不懂有没有弊端。

以下是代码

public class MobileService

{

    //NameSpace命名空间

    public final static String NameSpace = "http://WebXml.com.cn/";

    //SoapOption命名空间+方法名

    public final static String SoapOption = "http://WebXml.com.cn/getMobileCodeInfo";

    //MethodName将要调用的方法名

    public final static String MethodName = "getMobileCodeInfo";

    //WebServiceURL WebService地址

    public final static String WebServiceURL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";

    //定义一个SoapObject对象

    public SoapObject request = null;

   

    public String GetMobileAddress(String phoneNO)

    {

      request = new SoapObject(NameSpace, MethodName);

      request.addProperty("mobileCode", phoneNO);//存储过程方法参数,参数名与webservice的参数名顺序都需要一致

      request.addProperty("userID", "");

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

      envelope.bodyOut = request;

      envelope.dotNet = true;//指定为调用.net

      HttpTransportSE ht = new HttpTransportSE(WebServiceURL);

      try

      {

        ht.call(SoapOption, envelope);

      }

      catch (Exception e)

      {

        e.printStackTrace();

      }

      //获取数据

      try

      {

            Object result= (Object)envelope.getResponse();

            String str=result.toString();

            return str;//结果

      }

      catch (SoapFault e)

      {

           return null;

      }

    }

}

posted on   KimhillZhang  阅读(787)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
 
点击右上角即可分享
微信分享提示