首先什么是webservice ?
SOAP 1.2 以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。 POST /WebServices/MobileCodeWS.asmx HTTP/1.1 Host: webservice.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>
根据上面给出的xml得知,第一个xml文件是post要提交的数据,根据提示请求的数据需要用占位符
<mobileCode>$mobile</mobileCode> 这里给号码加入占位符
<userID></userID> 这个号码所对应的唯一编号,可以留空,因为不处理它
3.把post请求的xml文件拷贝到项目中,这里是拷贝到了src下面--post.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>
具体代码如下NumberService类--处理post请求以及获取服务器返回的归属地信息
public class NumberService { /** * 查询号码归属地 * * @param number * @return * @throws Exception */ public static String getAddress(String number) throws Exception { // 把soap肥皂协议-以post方式提交的xml数据获取出来 InputStream is = NumberService.class.getClassLoader() .getResourceAsStream("data.xml"); byte[] xml = StreamTool.getBytes(is); String postxml = new String(xml); //获取将要提交的xml中的 手机号,$mobile为号码占位符 --- 这个xml就相当于 一个jsp表单 String phoneNumber = postxml.replace("$mobile", number); //以post方式发送数据给服务器 String numberAddress = sendDataByPost(phoneNumber); return numberAddress; } /** * 以post方式请求服务器 * * @param result * 参数就是要查询的手机号码 * @return * @throws Exception */ public static String sendDataByPost(String phoneNumber) throws Exception { URL url = new URL( "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); String data = phoneNumber; // 要传递的数据 //byte[] bytes = data.getBytes(); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置超时连接 conn.setReadTimeout(5000); //设置请求方式 conn.setRequestMethod("POST"); //设置post请求的参数 conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); conn.setRequestProperty("Content-Length", data.length()+""); //允许http协议对外输出信息 conn.setDoOutput(true); //把数据写给服务器 conn.getOutputStream().write(data.getBytes()); int code = conn.getResponseCode(); if(code==200){ //获取从服务器中返回过来的流(由于返回的数据是xml格式,所以需要解析xml) InputStream is = conn.getInputStream(); String address = ResultXml.getAddress(is); return address; }else{ return "访问网络出错"; } } }
根据上面服务器给出的xml相应文件--需要解析getMobileCodeInfoResult就可以获取手机对属地信息
<?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>
解析类如下
/** * 解析服务器返回的xml数据,获取归属地 * @author Administrator * */ public class ResultXml { public static String getAddress(InputStream is) throws Exception{ XmlPullParser parser = Xml.newPullParser(); parser.setInput(is, "utf-8"); int type = parser.getEventType(); while(type!=XmlPullParser.END_DOCUMENT){ if(type==XmlPullParser.START_TAG){ if("getMobileCodeInfoResult".equals(parser.getName())){ return parser.nextText(); } } type = parser.next(); } return "没有该号码的信息"; } }
上面代码中使用了一个工具类StreamTool
public class StreamTool { /** * * @param is 参数InputStream代表从服务器返回过来的流(流中存储了我们所需的数据) * @return * @throws Exception * 这里把服务器流中的数据循环的写入到了内存数组流ByteArrayOutputStream里面 * 作用是内存操作数据快.最后以字节数组返回 */ public static byte[] getBytes(InputStream is) throws Exception{ ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while((len = is.read(buffer))!=-1){ bos.write(buffer, 0, len); } is.close(); bos.flush(); byte[] result = bos.toByteArray(); System.out.println(new String(result)); return result; } }
测试截图