调用webservice
发布时间:2020-12-16 23:21:22 所属栏目:安全 来源:网络整理
导读:首先先介绍一个webservice的免费的网站http://www.webxml.com.cn/zh_cn/index.aspx 一般的公共的webservice发布都会支持soap1.1与soap1.2两种但是我们做客户端请求时建议使用soap1.1的方式这是因为按soap1.2方式编写的webservice的服务端能处理soap1.1 的请
首先先介绍一个webservice的免费的网站http://www.webxml.com.cn/zh_cn/index.aspx 一般的公共的webservice发布都会支持soap1.1与soap1.2两种但是我们做客户端请求时建议使用soap1.1的方式这是因为按soap1.2方式编写的webservice的服务端能处理soap1.1 的请求。所以当我们自己发布webservice接口给别人用时,我们就要用soap1.2的方式。 ? ?webservice提供的soap格式给了我们webservice的服务地址 POST 服务地址 ? HTTP 以及请求头的其他如: Content-Type: Content-Length等,后面即soap的请求格式,我们只需修改参数即可。 下面是具体的调用方法(用的事soap1.2的方式): /* * 手机归属地查询 */ public static void mobilePhoneTest() throws IOException{ String weatherStr = weatherStr("1507487xxxx",""); //发布的服务器地址http://webservice.webxml.com.cn/+soap发布的HTTP POST 地址WebServices/MobileCodeWS.asmx URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); //开启链接 URLConnection conn = url.openConnection(); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8"); conn.setRequestProperty("Content-Length",Integer.toString(weatherStr.length())); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); writer.write(weatherStr); writer.flush(); writer.close(); InputStream in = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8")); String str = null; while((str=reader.readLine())!=null){ System.out.println(str); } reader.close(); } public static String weatherStr(String mobileCode,String userID){ String weatherStr = "<?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>"+mobileCode+"</mobileCode><userID>"+userID+"</userID></getMobileCodeInfo>"+ "</soap12:Body></soap12:Envelope>"; return weatherStr; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |