微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

调用webservice

首先先介绍一个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;
	}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐