加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

调用webservice

发布时间:2020-12-17 00:32:34 所属栏目:安全 来源:网络整理
导读:利用webservice查询手机号码归属地实现方法:Android客户端可以通过在Http的基础上使用SOAP调用webservice服务器上的API第一步:在src下新建一个xml文件.(1)从网站http://WebXml.com.cn/找到需要的API(2)把从网上拷贝的API即soap协议粘贴进刚才新建的xml文件(3
利用webservice查询手机号码归属地
实现方法:
Android客户端可以通过在Http的基础上使用SOAP调用webservice服务器上的API


第一步:在src下新建一个xml文件.
(1)从网站http://WebXml.com.cn/找到需要的API
(2)把从网上拷贝的API即soap协议粘贴进刚才新建的xml文件
(3)注意:在xml中使用占位符$mobile来代替还未输入的手机号码
soap.xml文件如下:
<?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>$mobile</mobileCode>
      <userID></userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>

第二步:发送和解析SOAP协议
注意:
(1)在读取soap.xml文件后,要将其中的占位符替换成需要查询的手机号码.
   因为$是正则表达式的特殊字符所以需要用转义,但是也是Java中的特殊字符所以还要对进转义.于是为$mobile
(2)传递给parseSOAP(InputStream xml)方法的参数是conn的输入流即parseSOAP(conn.getInputStream());

发送SOAP协议:
public class MobileService {
	public static String getAddress(String mobile) throws Exception {
		InputStream inStream = MobileService.class.getClassLoader().getResourceAsStream("soap.xml");
		byte[] data = StreamTool.read(inStream);//read(inStream)方法见下
		String content = new String(data,"UTF-8");
		String soap = content.replaceAll("$mobile",mobile);//替换占位符,形成真正的soap协议
		byte[] entity = soap.getBytes();		
		String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
		HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);
		conn.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
		conn.setRequestProperty("Content-Length",String.valueOf(entity.length));
		OutputStream outStream = conn.getOutputStream();
		outStream.write(entity);
		if(conn.getResponseCode() == 200){
			return parseSOAP(conn.getInputStream());//参数为输入流
		}
		return null;
	}

webservice服务器返回的soap协议如下:
<?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>
注意:
(1)<getMobileCodeInfoResult>string</getMobileCodeInfoResult>中的String是我们正需要得到的
(2)主要是利用XmlPullParser解析XML文件
解析SOAP协议:
private static String parseSOAP(InputStream xml) throws Exception{
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(xml,"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;
	}
}


//read(inStream)方法:
public class StreamTool {
	public static byte[] read(InputStream inStream) throws Exception{
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while( (len= inStream.read(buffer)) != -1 ){
			outStream.write(buffer,len);
		}
		outStream.close();
		inStream.close();
		return outStream.toByteArray();
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读