Wednesday, October 26, 2011

(502)Bad Gateway returned from web request

The solutions is to set the default proxy for the web request and assign user agent to it as shown below:
Code:
ListURL = String.Format("your_xml_feed_or_API_url_sample");  
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ListURL);  
request.Timeout = 900000000;  
request.Proxy = HttpWebRequest.DefaultWebProxy;  
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";  
HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
Stream dataStream = response.GetResponseStream();  
dataStream = response.GetResponseStream();  
StreamReader reader = new StreamReader(dataStream);  
ListingSource = reader.ReadToEnd();  
ListingSource = System.Web.HttpUtility.HtmlDecode(ListingSource);
Cheers!

0 comments:

Post a Comment