第二種使用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;
}
下面是第一種調用方式的傳送門,感興趣的朋友可以也去看看。