Java调用WebService(asmx)服务接口

 利用HttpURLConnection通过soap调用接口

public static void cl6(String xml) throws IOException {
		// 服务器地址
		String urlString = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
		// 需要调用的方法
		String soapActionString = "getMobileCodeInfo";
		URL url = new URL(urlString);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		// 拼接soap
		String soap = xml;
		byte[] buf = soap.getBytes("UTF-8");
		// 设置报头
		conn.setRequestProperty("Content-Length", String.valueOf(buf.length));
		conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
		conn.setRequestProperty("soapActionString", soapActionString);
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);
		conn.setDoInput(true);

		OutputStream out = conn.getOutputStream();
		out.write(buf);
		out.close();
		// 获取响应状态码
		int code = conn.getResponseCode();
		StringBuffer sb = new StringBuffer();
		if (code == HttpStatus.OK.value()) {
			InputStream is = conn.getInputStream();
			byte[] b = new byte[1024];
			int len = 0;
			while ((len = is.read(b)) != -1) {
				String s = new String(b, 0, len, "utf-8");
				sb.append(s);
			}
			is.close();
		}
		System.out.println(sb);
	}

	public static void main(String args[]) throws IOException {
		
		String str = 
					"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
					+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
					+ "  <soap:Body>\n" 
					+ "    <getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">\n"
					+ "      <mobileCode>18291876713</mobileCode>\n" 
					+ "      <userID></userID>\n"
					+ "    </getMobileCodeInfo>\n" 
					+ "  </soap:Body>\n" 
					+ "</soap:Envelope>";
		
		cl6(str);
	}

使用HttpClient

public static void cl3() throws IOException {
		// 输入服务网址
		HttpClient client = new HttpClient();
		// HttpClient client = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager(true));
		// GetMethod
		PostMethod post = new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
		// 设置参数
		post.setParameter("mobileCode", "18291876713");
		post.setParameter("userID", "");
		// client.setTimeout(newTimeoutInMilliseconds);

		// 执行,返回一个结果码
		int code = client.executeMethod(post);

		System.out.println("结果码:" + code);
		// 获取xml结果
		String result = post.getResponseBodyAsString();
		System.out.println("结果:" + result);
		// 释放连接
		post.releaseConnection();
		// 关闭连接
		((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
	}

使用jar

		<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
		</dependency>

 

转载于:https://my.oschina.net/pipimao/blog/1612466

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值