1

当我运行我的 Web 应用程序代码时,我在这一行得到了这个错误。

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

实际上,当我直接在浏览器上运行我的 url 时。它会给出正确的 o/p,但是当我在代码中运行我的 url 时。它会给出异常。

这里 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();            
  }   
4

2 回答 2

4

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

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

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

request.Method = "GET";

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

于 2013-01-22T12:09:15.957 回答
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; 
于 2013-11-07T09:43:46.473 回答