Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting different repsonses depending where HttpWebRequests are executed
    text
    copied!<p>I'm experiencing some weird issues with executing code in console application, in a MVC controller and in a class library. In the first two cases I get the response I expect (in this case a zip file) but when I try to execute the code in the class library (dependency in my MVC controller) I get a 302 HTTP Status in my response with a weird redirect to an error page.</p> <p>So the strange part is that it works in my console test application and in the MVC controller, but not as a dependency in my MVC project. Is there any difference executing this code in the controller versus in a class library as a dependency?</p> <p>I checked the requests and responses in Fiddler but everything is identical except of course ASP.NET SessionId, viewstate and the response content.</p> <pre class="lang-cs prettyprint-override"><code>var cookies = new CookieContainer(); var firstRequest = (HttpWebRequest)WebRequest.Create("UrlToAspx"); firstRequest.Method = "GET"; firstRequest.KeepAlive = false; firstRequest.CookieContainer = cookies; var firstResponse = firstRequest.GetResponse() as HttpWebResponse; var responseReader = new StreamReader(firstResponse.GetResponseStream()); var responseData = responseReader.ReadToEnd(); responseReader.Close(); var viewstate = ExtractViewstate(responseData); var eventvalidation = ExtractEventValidation(responseData); var postData = string.Format("__LASTFOCUS=&amp;__EVENTTARGET=&amp;__EVENTARGUMENT=&amp;__VIEWSTATE={0}&amp;__EVENTVALIDATION={1}&amp;ctl00%24main%24ResultFormatGroup={2}&amp;ctl00%24main%24DropDownList1={3}&amp;&amp;ctl00%24main%24fromDate={4}&amp;&amp;ctl00%24main%24tomDate={5}&amp;&amp;ctl00%24main%24ImageButton1.x={6}&amp;&amp;ctl00%24main%24ImageButton1.y={7}", viewstate, eventvalidation, "optExport", "Transaktioner", "2011-01-01", "2011-08-17", "7", "15"); var data = Encoding.UTF8.GetBytes(postData); var request = (HttpWebRequest)HttpWebRequest.Create("UrlToAspx"); request.Method = "POST"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.Headers.Add("Accept-Encoding", "gzip, deflate"); request.Headers.Add("Accept-Language", "sv-SE"); request.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"); request.Referer = "Referer"; request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; request.ContentType = "application/x-www-form-urlencoded"; request.Host = "referer"; request.Headers.Add("Pragma", "no-cache"); request.CookieContainer = cookies; request.KeepAlive = false; request.ContentLength = data.Length; var newStream = request.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); var secondStream = new StreamReader(request.GetResponse().GetResponseStream()); var realResponseData = secondStream.ReadToEnd(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload