调用http://WebXml.com.cn/的webservice获取手机号段信息

反正要使用wsdl4j.jar,axis.jar等一些jar包

soap协议规范

SOAP 1.1

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>string</mobileCode>
      <userID>string</userID>
    </getMobileCodeInfo>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    </getMobileCodeInfoResponse>
  </soap:Body>
</soap:Envelope>

 

SOAP 1.2

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>string</mobileCode>
      <userID>string</userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    </getMobileCodeInfoResponse>
  </soap12:Body>
</soap12:Envelope>

 

HTTP GET

以下是 HTTP GET 请求和响应示例。所显示的占位符需替换为实际值。

GET /WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string HTTP/1.1
Host: www.webxml.com.cn
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">string</string>

 

HTTP POST

以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebServices/MobileCodeWS.asmx/getMobileCodeInfo HTTP/1.1
Host: www.webxml.com.cn
Content-Type: application/x-www-form-urlencoded
Content-Length: length

mobileCode=string&userID=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">string</string>

 具体调用函数

public static String[] GetMobileMarkByWebService(String mobile7) {
  String[] mobileInfos = new String[] {};
  try {

  //调用webservice的地址
   String endPoint = "http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx";
   Service service = new Service();
   Call call = (Call) service.createCall();
   call.setTargetEndpointAddress(new URL(endPoint));

  //调用webservice方法,必须这样调用该方法,是根据soap协议里的方法声明
   call.setOperationName(new QName("http://webxml.com.cn/getMobileCodeInfo)); 

//webservice里method的参数,必须这样定义,否则必然出错

   call.addParameter(new QName("http://WebXml.com.cn/", "mobileCode"),XMLType.XSD_STRING, ParameterMode.IN);
   call.addParameter(new QName("http://WebXml.com.cn/", "userID"),XMLType.XSD_STRING, ParameterMode.IN);
   call.setReturnType(XMLType.XSD_STRING);
   call.setUseSOAPAction(true);
   call.setSOAPActionURI("http://WebXml.com.cn/getMobileCodeInfo");
   String result = (String) call.invoke(new Object[] { mobile7, "" });
   String[] infos = result.split(":", 2);
   if (infos.length == 2) {
    mobileInfos = infos[1].split(" ", 3);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return mobileInfos;
 }

唯一不爽的就是这个webservice只能免费使用50次每天每IP。

原文地址:https://www.cnblogs.com/zsxfbj/p/1748561.html