烦恼! POST 在 HttpWebRequest 中返回 302 发现对象移动错误

发布于 2024-09-14 08:06:25 字数 2430 浏览 5 评论 0原文

请有人帮忙 - 一直在努力解决这个糟糕的问题!

我正在做什么 - 我有一个 ASPX 页面,我从该页面发起 GET,然后 POST 到 HTTPS 页面,以便登录到该页面。我花了相当多的时间将我的 GET 和 POST 结构与使用 fiddler(协议分析器)的浏览器 GET/POST 进行比较,我的请求很好。

但是,当我尝试通过浏览器登录时,一切正常并且登录。当我运行页面时,我可以看到正确的 GET 和 POST,但我收到 302 发现“对象移动错误”

最初我以为这是一个cookie 问题,但经过多次实验,我很确定这与 cookie 无关。我已经在浏览器上禁用了 cookies 和 javascript 并尝试过,页面在没有任何一个的情况下都可以正常工作。然后我模拟了精确的 GET/POST。

这是我的情况:

  1. 我的 GET 和浏览器 GET 完全相同
  2. 来自站点的 200 OK 响应完全相同,除了三个 VIEWSTATE 变量的长度略有不同(为什么?即使 GET 相同为什么不同?)
  3. 我的 POST 和浏览器的 POST 完全一样,除了 3 个 Viewstate 变量(我从 GET 正确填写它)
  4. 但是,浏览器登录,而我收到 302 发现/对象移动错误。

其他一些事情 -

a) 我从最近的浏览器 POST 复制了 POST 响应,并用此浏览器 POST 替换了我的 POST 参数,这给了我正确的响应!这表明
- 我的标题很好
- 我的编码设置/环境等都很好
- VIEWSTATE 值中有可疑之处,这只能是因为浏览器首先将其发送给我(我解析 GET VIEWSTATE 变量并在 POST 中使用它时没有损坏,它完全没问题)

更新< /strong> 我也尝试过 WebClient 只是为了检查 - 没有区别,相同的 302。 更新 移动的对象基本上指向一个错误页面,上面写着“发生了严重错误等等” - POST 导致服务器出现错误,与良好的 POST(浏览器的)之间的唯一区别)和我的 POST 是 Viewstate 变量。

那么 - 我做错了什么?为什么这个残酷的世界要折磨我?!!

(PS - 浏览器序列中的另一个差异,不确定它有什么关系)

浏览器:
连接
获取
GET(对于网站图标,返回错误)
连接
POST(成功)
我:
连接
获取
POST(严重失败,302 - 页面已移动)

对于那些关心的人,我的 POST 标头构造代码

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

...然后读取流等。没有 cookie。

Someone please help - been struggling with this lousy problem!

What I'm doing - I have an ASPX page from which I originate a GET and then a POST to a HTTPS page with a view to login to it. I have spent quite a bit of time comparing my GET and POST construction to a browser GET/POST using fiddler (protocol analyzer) and my requests are fine.

However, when I try login through the browser, everything works fine and it logs in. When I run my page, I can see the correct GET and POST, but I get a 302 found 'object moved error'

Originally I thought this was a cookie issue, but after much experimentation I'm pretty sure this has nothing to do with cookies. I have disabled cookies AND javascript on the browser and tried, and the pages work fine without either. I then simulated the exact GET/POST.

This is my situation:

  1. My GET and the browsers GET are EXACTLY THE SAME
  2. The 200 OK response from the site is EXACTLY the same EXCEPT three VIEWSTATE variables which have slightly different lengths (why? why different even if GET is same?)
  3. My POST and the browsers POST are EXACTLY the same EXCEPT the 3 Viewstate variables (I fill it correctly from the GET)
  4. And yet, the browser logs in, while I get a 302 found / object moved errror.

A couple of other things -

a) I copied the POST response from a recent browser POST and replaced my POST params with this browser POST and that got me the right response! This indicates that
- my headers are just fine
- my coding setup / environment etc. are fine
- something fishy in the VIEWSTATE values, which can only be because the browser sent it to me in the first place (there is no corruption in my parsing the GET VIEWSTATE variables and using it in POST, it's perfectly fine)

update I have also tried WebClient just to check - no difference, same 302.
update The object moved basically points to a error page which says 'a serious error occurred blah blah' - the POST is causing a error at the server, and the ONLY difference between the good POST (of the browser) and my POST are the Viewstate variables.

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

(PS - one other difference in the browser sequence, not sure how it matters)


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 construction 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 stream etc. no cookies.

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

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

发布评论

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

评论(1

烟酉 2024-09-21 08:06:25

我终于想通了 - 希望其他遇到同样问题的人不必再次经历这个问题。大多数 HTTP 专家和熟悉 WWW 开发的人可能永远不会接触到它,但新手很可能会接触到它。

那么问题出在哪里呢?我已将问题范围缩小到我一直怀疑的 VIEWSTATE (请参阅上面的帖子...)。事实证明,我所要做的就是在将解析后的 VIEWSTATE 值放入 POST 之前对它们进行 Server.UrlEncode - 就是这样。我花了一整天的时间才做到这一点。

所以,作为其他新手的学习

  • 如果您尝试通过代码 POST 到页面并需要向其发送从 GET 解析的 VIEWSTATE 变量,那么首先在创建参数之前对其进行 Server.UrlEncode - 例如

  • 做 GET
  • 将响应流获取到字符串中
  • 解析字符串(我使用 HtmlAgilityPack-fabulous)
  • param1 = name +"="+Server.UrlEncode(value)+"&"
  • POST 参数 = 参数1+参数2+...
    - 在 POST 中发送 - 瞧,它可以工作

因为我从来没有用 HttpWebRequest 等进行过编程,我首先通过逐一消除 cookie、javascript、GET 构造、POST 构造来缩小问题范围-一个使用 fiddler(很棒的分析工具,免费),然后最后使用 BeyondCompare 进行字节比较,就在那时我发现了 VIEWSTATE 变量修改。

我学习了有关 URL 编码的课程,希望您不必学习!

I finally figured it out - and hopefully someone else who chances upon the same problem does not have to go through this again. It's possible that most HTTP gurus and people familiar with WWW development would never hit it, but a newbie quite well could.

So what was the problem? I had narrowed down the problem to VIEWSTATE which I always suspected (see my post above...). It turns out that all I had to do was to Server.UrlEncode the parsed VIEWSTATE values before putting them onto POST - that's it. It took me all day to get to that.

SO, as a learning to other newcomers

  • If you are trying to POST to a page through code and need to send it VIEWSTATE variables that you parsed from GET, then first Server.UrlEncode it before creating the parameters - for e.g.

  • 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 have never, ever programmed with HttpWebRequest etc., I started by narrowing down my problem by eliminating cookies, javascript, GET construction, POST construction one-by-one using fiddler (great analyzer tool, free) and then finally did byte-comparison using BeyondCompare, and that's when I caught the VIEWSTATE variable modifications.

I learnt a lesson on URL encoding, and hopefully you won't have to!

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