R – Cookies are required to use this site. HttpWebRequest/HttpWebResponse Cookies

cookieshttpwebrequesthttpwebresponserss

I am using the following code to make a HttpWebRequest and render the XML from the response stream.

`
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);

req.Accept = "/";

req.Headers.Add("UA-CPU", "x86");

req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; MS-RTC LM 8)";

req.CookieContainer = new CookieContainer();

using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())

{

using (StreamReader reader = new StreamReader(resp.GetResponseStream()))

{

  sb.Append(reader.ReadToEnd());

}

}`

It was working fine on my local server and test server but on UAT server, i am getting the response as 'Cookies are required to use this site'. I don't understand why this is happenning. If I browse to the URL directly, valid XML is getting rendered but it doesn't work if use the above code. Can anyone help, please?

Best Answer

It sounds like the sever is expecting a cookie that you aren't sending. After you browse to the URL in the browser, type this in the location bar to see the cookies: javascript:alert(document.cookie);

Related Topic