调用webservice查询手机号归属地
2015-04-02 15:08
183 查看
public static String getAddress(String mobildeCode) { String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; String soap = readSoap(); soap = soap.replaceAll("\\$mobile", mobildeCode); try { HttpURLConnection conn = (HttpURLConnection) new URL(path) .openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8"); conn.setRequestProperty("Content-Length", String.valueOf(soap.length())); conn.getOutputStream().write(soap.getBytes()); if (conn.getResponseCode() == 200) { String result = parseSOAP(conn.getInputStream()); return result; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
private static String parseSOAP(InputStream xml) throws Exception { // TODO Auto-generated method stub 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; } private static String readSoap() { InputStream stream = LoginService.class.getClassLoader() .getResourceAsStream("soap.xml"); String str = readStreamToStr(stream); return str; } private static String readStreamToStr(InputStream stream) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len = 0; byte[] buffer = new byte[1024]; try { while ((len = stream.read(buffer)) != -1) { baos.write(buffer, 0, len); } String result = new String(baos.toByteArray(), "UTF-8"); return result; } catch (Exception e) { e.printStackTrace(); } return null; }
//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>
相关文章推荐
- 阅读笔记:x86系统调用入门
- 一个比较容易调用 功能比较完善的 cookie 操作类 附带一些简单的应用示例
- C#调用Response.Redirect方法触发异常问题
- iframe 父窗口和子窗口相互的调用方法集锦
- js调用java webservice备忘
- 在Delphi中宿主.NET运行环境,直接调用.NET中的代码
- My97DatePicker时间控件和编辑器的调用
- 使用JQuery从客户端调用C#方法
- android中调用分享程序的代码
- 后台调用js的方法
- Qt调用Delphi编写的COM组件
- WinCE USB 驱动中IssueTransfer调用相关
- 在插件中调用表单上配置的业务逻辑和控制页签控件
- java调用kettle中的资源库文件(job\trans)
- c#&nbsp;WinForm调用控制台
- centos下C编程调用libvirt的API访问KVM虚拟机
- UIView的layoutSubviews和drawRect方法何时调用
- arguments.callee 调用自身
- LoadRunner调用Oracle存储过程
- 编写ATL工程实现ActiveX控件调用cryptoAPI接口(四)------------Base64转码