[英]The remote server returned an error: (405) Method Not Allowed in expedia api call
string url = "https://book.api.ean.com/ean-services/rs/hotel/v3/avail?cid=55505&minorRev=30&apiKey=bsm4nqpcawnppkpwjkej2rfq&locale=en_US¤cyCode=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();
Your server requires a GET
not POST
.您的服务器需要
GET
而不是POST
。 Below code works下面的代码有效
var url = "https://book.api.ean.com/ean-services/rs/hotel/v3/avail?cid=55505&minorRev=30&apiKey=bsm4nqpcawnppkpwjkej2rfq&locale=en_US¤cyCode=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);
}
if you want the result as json instead of xml, just comment out the line如果您希望结果为 json 而不是 xml,只需注释掉该行
wc.Headers.Add("Accept", "application/xml");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.