http client 方式调用webservice

本文介绍了一种利用SOAPUI插件简化SOAP请求报文构造的方法,并提供了通过Java实现的具体示例代码,演示了如何使用URLConnection调用Webservice服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于初学者而言,拼装soap请求报文似乎不是很简单的事情,但这里面有一个简单的方式获得soap报文,就是通过soapui插件,可以获得请求报文,具体了解soapui,这里不赘述,上代码

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 通过UrlConnection调用Webservice服务
*
*/
public class HttpClientTest {

public static void main(String[] args) throws Exception {
//服务的地址
URL wsUrl = new URL("http://localhost:8080/server/plus?wsdl");

HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();

conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");

OutputStream os = conn.getOutputStream();


String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://server/\">"+
"<soapenv:Header/>"+
"<soapenv:Body>"+
"<ser:add>"+
"<arg0>5</arg0>"+
"<arg1>6</arg1>"+
"</ser:add>"+
"</soapenv:Body>"+
"</soapenv:Envelope>";

os.write(soap.getBytes());
InputStream is = conn.getInputStream();

byte[] b = new byte[1024];
int len = 0;
String s = "";
while((len = is.read(b)) != -1){
String ss = new String(b,0,len,"UTF-8");
s += ss;
}
System.out.println(s);

is.close();
os.close();
conn.disconnect();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值