根据 HttpWebRequest 的执行位置获得不同的响应

发布于 2024-12-04 09:13:55 字数 2353 浏览 0 评论 0原文

我在控制台应用程序、MVC 控制器和类库中执行代码时遇到一些奇怪的问题。在前两种情况下,我得到了我期望的响应(在本例中是一个 zip 文件),但是当我尝试执行类库中的代码(MVC 控制器中的依赖项)时,我在响应中得到了 302 HTTP 状态,并带有奇怪的内容重定向到错误页面。

因此,奇怪的是它可以在我的控制台测试应用程序和 MVC 控制器中工作,但不能作为我的 MVC 项目中的依赖项。在控制器中执行此代码与在类库中作为依赖项执行此代码有什么区别吗?

我检查了 Fiddler 中的请求和响应,但一切都是相同的,当然除了 ASP.NET SessionId、视图状态和响应内容。

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=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE={0}&__EVENTVALIDATION={1}&ctl00%24main%24ResultFormatGroup={2}&ctl00%24main%24DropDownList1={3}&&ctl00%24main%24fromDate={4}&&ctl00%24main%24tomDate={5}&&ctl00%24main%24ImageButton1.x={6}&&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();

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.

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?

I checked the requests and responses in Fiddler but everything is identical except of course ASP.NET SessionId, viewstate and the response content.

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=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE={0}&__EVENTVALIDATION={1}&ctl00%24main%24ResultFormatGroup={2}&ctl00%24main%24DropDownList1={3}&&ctl00%24main%24fromDate={4}&&ctl00%24main%24tomDate={5}&&ctl00%24main%24ImageButton1.x={6}&&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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-12-11 09:13:55

将主机标头更改为您尝试访问的域,而不是使用引用站点。

Change the host header to the domain you are trying to access instead of using a referer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文