WebRequest跟随重定向 [英] WebRequest follow redirect

查看:143
本文介绍了WebRequest跟随重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面(URL a),该页面使用WebRequest以编程方式提交表单.
接受表单请求的页面会执行很多操作,然后重定向到另一个页面(url b)

I have a page (url a) which submits a form programmatically using a WebRequest.
The page that accepts the form request does a bunch of stuff and then redirects to another page (url b)

完全有可能执行webrequest(读取,处理和重定向表单请求),然后使当前页面(URL a)重定向到webrequest结束位置(URL b).

Is it possible at all to perform the webrequest (which reads, processes and redirects the form request), which then makes the current page (url a) redirect to the webrequest end location (url b).

我目前有;

  HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url + postVars);  
  myRequest.AllowAutoRedirect = true;

  HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();  
  myRequest.AllowAutoRedirect = true;

  // Read response stream
  StreamReader myStream = new StreamReader(myResponse.GetResponseStream());  
  string response = myStream.ReadToEnd();  
  myResponse.Close();

一切正常,除了myResponse.Close()之后,一切都按预期进行,我仍然在URL a而不是URL b,即发出请求的页面仍然是活动页面

This all works fine and everything happens as expected apart from after the myResponse.Close(), I am still at url a, not url b, i.e the page that the request was made from is still the active page

关于我要去哪里的任何想法?还是更好的主意?

Any ideas on where I am going wrong? Or any better ideas?

我需要能够.

  • 打开页面
  • 以编程方式向模块b(另一页)提交表单
  • 重定向到页面c(其中基于模块b的重定向)

这有意义吗?
预先感谢.

Does that make sense?
Thanks in advance.

推荐答案

我认为您对重定向的工作方式感到困惑-您向 URL A 发出了 request 如果您在此处重定向(并且AllowAutoRedirect设置为true),则最终可能会从网址B 收到响应.

I think you are confused about how redirects work - you make a request to Url A, but may ultimately receive a response from Url B if you get redirected there (and AllowAutoRedirect is set to true).

客户/请求者不会以任何形状或形式被重定向,而只是客户请求的响应来自哪里.因此,您的活动页面"当然不会改变.

The client / requestor does not get redirected in any shape or form, just where the response to a client's request is coming from is. So your "active page" of course never changes.

这篇关于WebRequest跟随重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆