Annoyingly! POST returns 302 found objects moved to HttpWebRequest

Someone, please help - struggled with this lousy problem!

What I do - I have an ASPX page from which I create a GET, and then POST to an HTTPS page to enter it. I spent a lot of time comparing my GET and POST constructs with the GET / POST browser using a violinist (protocol analyzer) and my requests are fine.

However, when I try to log into the browser, everything works fine and it logs in. When I start my page, I can see the correct GET and POST, but I get the 302 "moved object" error found

Initially, I thought it was a cookie problem, but after many experiments I am sure that this has nothing to do with cookies. I turned off cookies and javascript in the browser and tried, and the pages worked fine without them. Then I simulated the exact GET / POST.

This is my situation:

  • My GET and GET Browsers EXACTLY VERY
  • The 200 OK response from the site is EXACTLY the same EXCEPT for three VIEWSTATE variables that have slightly different lengths (why? Why different, even if the GET is the same?)
  • POST POST and POST browsers EXACTLY EXCLUDE 3 ViewState variables (I correctly populate them from GET)
  • And yet the browser is logging in while I get a detected 302 detected / moved error message.

A few other things -

a) I copied the POST response from a recent POST browser and replaced the POST settings with this POST browser and got the correct answer! That means - my headlines are just good
- my encoding setting / environment, etc. - something suspicious of VIEWSTATE values, which can only be because the browser sent it to me first (there is no distortion when parsing GET VIEWSTATE variables and using it in POST, this is completely normal)

update I also tried WebClient just to check - it makes no difference, the same 302. update The moved object basically points to the error page that says: “a serious blah blah error occurred” - POST causes an error on the server, and ONLY the difference between a good POST (browser), and my POST are Viewstate variables.

So - WHAT AM I WRONG? Why is this cruel world tormenting me ?! !!

(PS is another difference in the browser sequence, I don’t know how important this is)

Browser:
CONNECT
GET
GET (for a favicon, which returns an error)
CONNECT
POST (success)
Me:
CONNECT
GET
POST (flaming failure, 302 - page moved)

and for those who care, my POST header build code

  HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL); myRequest.UserAgent = chromeUserAgent; //myRequest.CookieContainer = cCookies; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.Accept = chromeAccept; myRequest.Referer = url; myRequest.AllowAutoRedirect = false; myRequest.Host = "thesitethatskillingme.com"; myRequest.Headers.Add("Origin", "https://thesitethatskillingme.com"); myRequest.Headers.Add("Accept-Encoding", chromeAcceptEncoding); myRequest.Headers.Add("Accept-Language", chromeAcceptLanguage); myRequest.Headers.Add("Accept-Charset", chromeAcceptCharset); myRequest.Headers.Add("Cache-Control", "max-age=0"); myRequest.ServicePoint.Expect100Continue = false; myRequest.Method = "POST"; myRequest.KeepAlive = true; ASCIIEncoding ascii = new ASCIIEncoding(); byte[] bData = ascii.GetBytes(data); myRequest.ContentLength = bData.Length; using (Stream oStream = myRequest.GetRequestStream()) oStream.Write(bData, 0, bData.Length); 

... and then read the stream, etc. cookie

+2
source share
1 answer

I finally figured it out - and hopefully someone else who has a chance of the same problem should not repeat it again. It is possible that most HTTP gurus and people familiar with WWW development will never fall into it, but a newbie may well.

So what was the problem? I narrowed down the issue to VIEWSTATE, which I always suspected (see My post above ...). It turns out that all I had to do was Server.UrlEncode to parse the VIEWSTATE values ​​before putting them on POST - that is. It took me a whole day to get to this.

SO how to teach other beginners

  • If you are trying to send POST to the page using code and you need to send the VIEWSTATE variables that you analyzed from GET, then first Server.UrlEncode before creating the parameters - for example,

  • do get
  • get the response stream into a string
  • parse the string (I use HtmlAgilityPack- fabulous)
  • param1 = name + "=" + Server.UrlEncode (value) + "&"
  • POST param = param1 + param2 + ... -send this in POST - voila, it works

because I never programmed with HttpWebRequest, etc., I started by narrowing my problem by excluding cookies, javascript, a GET construct, building a POST one by one using a violinist (great analyzer tool, free), and then finally , made a byte comparison using BeyondCompare, and that when I caught the VIEWSTATE modification.

I’ve learned a URL encoding lesson and hopefully you don’t have to!

+4
source

All Articles