System.Net.WebException:远程服务器返回错误:(405)执行Web请求期间发生方法不允许.exception

时间:2013-01-22 11:55:36

标签: asp.net web-services

当我运行我的Web应用程序代码时,我在此行中收到此错误。

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()){}

实际上,当我直接在浏览器上运行我的网址时,它会提供正确的o / p,但是当我在代码中运行我的网址时。它会给出例外。

这里的MyCode是: -

string service = "http://api.ean.com/ean-services/rs/hotel/";

string version = "v3/";
string method = "info/";
string hotelId1 = "188603";

int hotelId = Convert.ToInt32(hotelId1);


string otherElemntsStr = "&cid=411931&minorRev=[12]&customerUserAgent=[hotel]&locale=en_US&currencyCode=INR";

string apiKey = "tzyw4x2zspckjayrbjekb397";
string sig = "a6f828b696ae6a9f7c742b34538259b0";

string url = service + version + method + "?&type=xml" + "&apiKey=" + apiKey + "&sig=" + sig + otherElemntsStr + "&hotelId=" + hotelId;



 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url) as HttpWebRequest;

 request.Method = "POST";
 request.ContentType = "text/xml";
 request.ContentLength = 0;

 XmlDocument xmldoc = new XmlDocument();

 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
 {
       StreamReader responsereader = new StreamReader(response.GetResponseStream());

       var responsedata = responsereader.ReadToEnd();
       xmldoc = (XmlDocument)JsonConvert.DeserializeXmlNode(responsedata);
       xmldoc.Save(@"D:\FlightSearch\myfile.xml");
       xmldoc.Load(@"D:\FlightSearch\myfile.xml");

       DataSet ds = new DataSet();
       ds.ReadXml(Request.PhysicalApplicationPath + "myfile.xml");
       GridView1.DataSource = ds.Tables["HotelSummary"];
       GridView1.DataBind();            
  }   

2 个答案:

答案 0 :(得分:3)

错误提供了您所需要的一切。

api或此调用可能不支持POST方法。

这应该有效。尝试将方法更改为“GET”

request.Method = "GET";

在浏览器中,您正在向api发送GET请求。您也应该在代码中执行相同的操作。

答案 1 :(得分:0)

对于XML响应:

request.Method = "GET";
request.ContentType = "text/xml; charset=UTF-8";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; BOIE9;ENUS)";
request.Accept = "application/xml";
request.ContentLength = 0;