我正在使用HttpWebRequest从TravelPort Universal 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) 错误请求。我不想使用 WSDL api 文件来获取数据。我想要通过httprequests的数据。请帮助我,或者我错过了什么.???
都是字节:
def make_basic_auth_header(username, password):
return ('Authorization',
b'Basic ' + base64.b64encode(
username.encode('ASCII') + b':' +
password.encode('ASCII')))