import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class TouricoTest {
/**
* 请求超时
*/
public static final int CONNECT_TIMEOUT=90000;
/**
* 读取超时
*/
public static final int READ_TIMEOUT=120000;
public static void main(String[] args) {
String result = "";
CloseableHttpClient httpclient=null;
CloseableHttpResponse response=null;
try{
String serverStr = "http://demo-hotelws.touricoholidays.com/HotelFlow.svc?wsdl"; //接口请求地址
httpclient = HttpClients.custom().setDefaultRequestConfig(getRequestConfig(CONNECT_TIMEOUT, READ_TIMEOUT)).build();
HttpPost httppost = new HttpPost(serverStr);
String requestParamter = getParamXml();//请求参数
HttpEntity stringEntry = new StringEntity(requestParamter, "utf-8");
httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
String wsRequestStr = "SearchHotelsById";//webservice 请求指定接口方法
httppost.setHeader("SOAPAction", wsRequestStr);
httppost.setHeader("Expect", "100-continue");
httppost.setHeader("Accept-Encoding", "gzip,deflate");
httppost.setHeader("Connection", "Keep-Alive");
httppost.setEntity(stringEntry);
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
int status = response.getStatusLine().getStatusCode();
if ((status >= 200 && status < 300)) {
result = EntityUtils.toString(entity);
} else {
result=null;
}
}
}
catch(Exception ex){
result=null;
ex.printStackTrace();
}finally{
if(null!=response){
try {
response.close();
}catch (IOException e) {
e.printStackTrace();
}
}
if(null!=httpclient){
try {
httpclient.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
//接口响应返回接口
System.out.println(result);
}
private static RequestConfig getRequestConfig(int connectTimeout,int readTimeout){
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(connectTimeout)
.setConnectTimeout(connectTimeout)
.setConnectionRequestTimeout(readTimeout)
.build();
return defaultRequestConfig;
}
private static String getParamXml(){
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append("Tu0906 111111 en_US 5 \n");
sb.append("2016-11-19 ");
sb.append("2016-11-21 4 0 ");
sb.append("0 ");
sb.append("0 1 ");
sb.append(" ");
return sb.toString();
}
上述是一份完整的wsdl 调用代码. 但是调不通. 请高手帮我诊断一下问题在哪?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
ringa_lee