WebRequest follow redirect

筅森魡賤 提交于 2019-12-08 04:16:23

问题


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)

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).

I currently have;

  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();

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?

I need to be able to.

  • Open page a
  • submit a form programmatically to module b (another page)
  • Be redirected to page c (of which is based on the redirect from module b)

Does that make sense?
Thanks in advance.


回答1:


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.



来源:https://stackoverflow.com/questions/8260736/webrequest-follow-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!