0

我正在使用 HttpWebRequest 从 TravelPort 通用 API 获取数据。你可以看到下面的代码:

public static string postXMLData( string requestXml)
    {
        string destinationUrl = "https://emea.universal-api.pp.travelport.com/B2BGateway/connect/uAPI";
        try
        {


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
            request.KeepAlive = false;
            request.SendChunked = true;
            request.AllowAutoRedirect = true;

            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
            //request.ContentType = "text/xml; encoding='utf-8'";
            request.ContentType = "text/xml";
            request.ContentLength = bytes.Length;
            request.Method = "POST";
            String username = "myUsername";
            String password = "MyPassword";
            //String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));

            string credentials = String.Format("{0}:{1}", username, password);
            byte[] bytess = Encoding.ASCII.GetBytes(credentials);
            string base64 = Convert.ToBase64String(bytess);
            string authorization = String.Concat("Basic ", base64);
            request.Headers.Add("Authorization", authorization);

            //request.Headers.Add("Authorization", "Basic " + encoded);

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = response.GetResponseStream();
                string responseStr = new StreamReader(responseStream).ReadToEnd();
                return responseStr;
            }
            return null;
        }
        catch(Exception ex)
        {
            return ex.Message;
        }
    }

我的请求 XML 字符串是:

  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">   
<soapenv:Header/>   
<soapenv:Body>      
<air:LowFareSearchReq xmlns:air="http://www.travelport.com/schema/air_v29_0" AuthorizedBy="user" SolutionResult="true" TargetBranch="P107788" TraceId="trace">         
<com:BillingPointOfSaleInfo xmlns:com="http://www.travelport.com/schema/common_v29_0" OriginApplication="UAPI"/>         <air:SearchAirLeg>            
<air:SearchOrigin>               
<com:Airport xmlns:com="http://www.travelport.com/schema/common_v29_0" Code="DXB"/>            
</air:SearchOrigin>            
<air:SearchDestination>              
 <com:Airport xmlns:com="http://www.travelport.com/schema/common_v29_0" Code="LHE"/>            
</air:SearchDestination>            
<air:SearchDepTime PreferredTime="2017-11-30"/>         
</air:SearchAirLeg>         
<air:AirSearchModifiers>            
<air:PreferredProviders>               
<com:Provider xmlns:com="http://www.travelport.com/schema/common_v29_0" Code="1P"/>            
</air:PreferredProviders>         
</air:AirSearchModifiers>         
<com:SearchPassenger xmlns:com="http://www.travelport.com/schema/common_v29_0" BookingTravelerRef="gr8AVWGCR064r57Jt0+8bA==" Code="ADT"/>      
</air:LowFareSearchReq>   
</soapenv:Body>
</soapenv:Envelope>

但是我收到错误消息,因为远程服务器返回了错误: (400) Bad Request。我不想使用 WSDL api 文件来获取数据。我想通过 httprequests 获取数据。请帮助我,或者我有什么遗漏的吗????

4

1 回答 1

3

都是字节:

def make_basic_auth_header(username, password):
        return ('Authorization',
            b'Basic ' + base64.b64encode(
                username.encode('ASCII') + b':' +
                password.encode('ASCII')))
于 2018-01-23T10:51:13.533 回答