第二种使用java语言调用webservice方法如下:
第二种方法使用的httpclient的方法,需要引入的包为apache的包
import org.apache.*
下面是测试代码:
public String updateLeechdom() throws Exception
{
//首先定义访问的格式和头,这部分的由来最简单的办法就是直接将对方提供的webservice地址在浏览器中访问获取
//注意用转义符处理特殊符号
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?> ";
xml = xml + "<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/\">";
xml = xml + "<soap:Body>";
xml = xml + "<function1 xmlns=\"namespace\">";
xml = xml + "<para1>" + para1 + "</para1>";
xml = xml + "<para2>" + para2 + "</para2>";
xml = xml + "</function2>";
xml = xml + "</soap:Body>";
xml = xml + "</soap:Envelope>";
//用来盛放返回值
String result ="";
PostMethod postMethod = new PostMethod(webServiceURL);
HttpClientParams httpClientParams = new HttpClientParams();
//设置链接的访问时间
httpClientParams.setConnectionManagerTimeout(999999);
//设置超时时间
httpClientParams.setSoTimeout(999999);
//创建http线程
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpClient httpClient = new HttpClient(httpClientParams,connectionManager);
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(11, true));
try
{
//设置返回值的编码格式
postMethod.setRequestEntity(new StringRequestEntity(xml, "text/xml", "GBK"));
postMethod.addRequestHeader("Connection", "close");
//设置一个返回的状态值用以判断是否调用webservice成功
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
} else {
//下面还是老规矩进行流和字符串之间的转换
InputStream out = postMethod.getResponseBodyAsStream();
BufferedReader in = new BufferedReader(new InputStreamReader(out));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null){
buffer.append(line);
}
result = buffer.toString();
}
} catch (Exception e) {
} finally {
postMethod.releaseConnection();
}
return result;
}
下面是第一种调用方式的传送门,感兴趣的朋友可以也去看看。
http://www.cnblogs.com/Sabasdian/p/JavaAndWebservice_1.html