简体   繁体   中英

Send httpwebrequest to TravelPort gives error The remote server returned an error: (400) Bad Request

I am using HttpWebRequest to get data from TravelPort Universal API. You can see the code below:

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;
        }
    }

And my Request XML string is:

  <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>

But I'm getting the error message as The remote server returned an error: (400) Bad Request . I don't want to use the WSDL api files to get data. I want data through httprequests. Kindly help me, or is there anything that I'm missing.???

It is all bytes:

def make_basic_auth_header(username, password):
        return ('Authorization',
            b'Basic ' + base64.b64encode(
                username.encode('ASCII') + b':' +
                password.encode('ASCII')))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM