C# 远程服务器返回错误:(405)expedia api调用中不允许使用方法

C# 远程服务器返回错误:(405)expedia api调用中不允许使用方法,c#,web-services,C#,Web Services,您的服务器需要GET not POST。下面的代码有效 string url = "https://book.api.ean.com/ean-services/rs/hotel/v3/avail?cid=55505&minorRev=30&apiKey=bsm4nqpcawnppkpwjkej2rfq&locale=en_US&currencyCode=INR&xml=%3CHotelRoomAvailabilityRequest%3E%0A%20%20%

您的服务器需要GET not POST。下面的代码有效

string url = "https://book.api.ean.com/ean-services/rs/hotel/v3/avail?cid=55505&minorRev=30&apiKey=bsm4nqpcawnppkpwjkej2rfq&locale=en_US&currencyCode=INR&xml=%3CHotelRoomAvailabilityRequest%3E%0A%20%20%20%20%3ChotelId%3E106347%3C%2FhotelId%3E%0A%20%20%20%20%3CarrivalDate%3E9%2F29%2F2015%3C%2FarrivalDate%3E%0A%20%20%20%20%3CdepartureDate%3E10%2F1%2F2015%3C%2FdepartureDate%3E%0A%20%20%20%20%3CincludeDetails%3Etrue%3C%2FincludeDetails%3E%0A%20%20%20%20%3CRoomGroup%3E%0A%20%20%20%20%20%20%20%20%3CRoom%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CnumberOfAdults%3E2%3C%2FnumberOfAdults%3E%0A%20%20%20%20%20%20%20%20%3C%2FRoom%3E%0A%20%20%20%20%3C%2FRoomGroup%3E%0A%3C%2FHotelRoomAvailabilityRequest%3E";
string xmlpath = url;

// Create a request using a URL that can receive a post. 
WebRequest request = WebRequest.Create(xmlpath);

// Set the Method property of the request to POST.
request.Method = "POST";

// Create POST data and convert it to a byte array.
string postData = xmlpath;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

// Set the ContentType property of the WebRequest.
request.ContentType = "application/json";

// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;

// Get the request stream.
Stream dataStream = request.GetRequestStream();

// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);

// Close the Stream object.
dataStream.Close();

// Get the response.
WebResponse response = request.GetResponse();
如果希望结果是json而不是xml,只需注释掉该行即可

 var url = "https://book.api.ean.com/ean-services/rs/hotel/v3/avail?cid=55505&minorRev=30&apiKey=bsm4nqpcawnppkpwjkej2rfq&locale=en_US&currencyCode=INR&xml=%3CHotelRoomAvailabilityRequest%3E%0A%20%20%20%20%3ChotelId%3E106347%3C%2FhotelId%3E%0A%20%20%20%20%3CarrivalDate%3E9%2F29%2F2015%3C%2FarrivalDate%3E%0A%20%20%20%20%3CdepartureDate%3E10%2F1%2F2015%3C%2FdepartureDate%3E%0A%20%20%20%20%3CincludeDetails%3Etrue%3C%2FincludeDetails%3E%0A%20%20%20%20%3CRoomGroup%3E%0A%20%20%20%20%20%20%20%20%3CRoom%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CnumberOfAdults%3E2%3C%2FnumberOfAdults%3E%0A%20%20%20%20%20%20%20%20%3C%2FRoom%3E%0A%20%20%20%20%3C%2FRoomGroup%3E%0A%3C%2FHotelRoomAvailabilityRequest%3E";

using (var wc = new WebClient())
{
    wc.Headers.Add("Accept", "application/xml");
    var str = wc.DownloadString(url);
}
wc.Headers.Add("Accept", "application/xml");