検索

ホームページ  >  に質問  >  本文

python - java httpClient wsdl 调用失败.求高手看下我代码?

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("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:aut=\"http://schemas.tourico.com/webservices/authentication\" xmlns:hot=\"http://tourico.com/webservices/hotelv3\" xmlns:hot1=\"http://schemas.tourico.com/webservices/hotelv3\">");
    sb.append("<soapenv:Header><aut:AuthenticationHeader><aut:LoginName>Tu0906</aut:LoginName><aut:Password>111111</aut:Password><!--Optional:--> <aut:Culture>en_US</aut:Culture><!--Optional:--><aut:Version>5</aut:Version></aut:AuthenticationHeader></soapenv:Header>\n");
    sb.append("<soapenv:Body><hot:SearchHotelsById><hot:request><hot1:HotelIdsInfo><hot1:HotelIdInfo id=\"964\"/></hot1:HotelIdsInfo><hot1:CheckIn>2016-11-19</hot1:CheckIn>");
    sb.append("<hot1:CheckOut>2016-11-21</hot1:CheckOut><hot1:RoomsInformation><hot1:RoomInfo><hot1:AdultNum>4</hot1:AdultNum><hot1:ChildNum>0</hot1:ChildNum>");
    sb.append("<hot1:ChildAges><hot1:ChildAge age=\"0\"/><hot1:ChildAge age=\"0\"/></hot1:ChildAges></hot1:RoomInfo></hot1:RoomsInformation><hot1:MaxPrice>0</hot1:MaxPrice>");
    sb.append("<hot1:StarLevel>0</hot1:StarLevel><hot1:AvailableOnly>1</hot1:AvailableOnly></hot:request><hot:features><hot:Feature name=\"?\" value=\"?\"/></hot:features>");
    sb.append("</hot:SearchHotelsById></soapenv:Body></soapenv:Envelope>");

    return sb.toString();
}

上述是一份完整的wsdl 调用代码. 但是调不通. 请高手帮我诊断一下问题在哪?

ringa_leeringa_lee2805日前1105

全員に返信(0)返信します

応答なし
  • キャンセル返事