HTTPS C # POST 302 Moved

I am trying to create a script that goes to a web page and captures a report - everything works GREAT, except - I get

HTTP/1.1 302 MovedTemporarily Date: Mon, 22 Jun 2009 13:22:04 GMT Server: Server x-some-id-1: 0J3X3VBBCGNJG9V46G5D x-some-id-2: BtQ4SsDhbryWgiVNFcVpMbt898GuPIBaWuGwAWjvsyI= Set-cookie: session-id-time=1246258800l; path=/; domain=.example.com; expires=Mon Jun 29 07:00:00 2009 GMT Set-cookie: session-id=179-5933843-4704124; path=/; domain=. example.com; expires=Mon Jun 29 07:00:00 2009 GMT Location: https://example.com Vary: Accept-Encoding,User-Agent nnCoection: close Content-Type: text/html; charset=UTF-8 Content-Length: 0 

The answer, and I have no idea how to stop it. I tried the settings

httpwebrequest.allowautoredirect is both "True" and "False", and nothing helps.

He's trying to go crazy, since I can log in to the site via https: //, but then will I get it back?

+4
source share
3 answers

I lingered on this problem for a long time - so glad I can help. Read article

http://www.byteblocks.com/page/How-to-submit-requests-to-web-sites-programatically-using-HttpWebRequest.aspx

The key issue is that you cannot use HttpWebRequest with automatic redirects turned on to complete the 302 login process and cookies, because cookies are not set until the end of the process.

The solution is to turn off automatic redirects and implement the entire login process manually in stages (get the "Location" header from 302 redirection responses, as well as the "Set-cookie" header and pass as necessary to the next steps).

In your cookie container you need to collect all the cookies and send them at the end. If you get 302, you will bang your head against the wall, wondering why you continue to end on the login page.

+4
source

I know this question is old, but google points are here. So here is another solution for WebClient .

 public class CookieAwareWebClient : WebClient { private CookieContainer cookie = new CookieContainer(); protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); if (request is HttpWebRequest) { (request as HttpWebRequest).CookieContainer = cookie; (request as HttpWebRequest).UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1300.0 Iron/23.0.1300.0 Safari/537.11"; } return request; } } 

Then create a WebClient object CookieAwareWebClient wc = new CookieAwareWebClient(); and do whatever you need.

Edit: also works via HTTP and HTTPS.

+2
source

Source: https://habr.com/ru/post/1286581/


All Articles