HTTPS C# POST 302 已移动

发布于 2024-07-25 08:30:36 字数 753 浏览 1 评论 0原文

我正在尝试创建一个登录网页并获取报告的脚本 - 一切都运行良好,除了 - 我收到响应

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

,但我不知道如何阻止它。 我尝试将

httpwebrequest.allowautoredirect 设置为“True”和“False”,但没有任何帮助。

这让我很抓狂,因为我可以通过 https:// 登录该网站,但后来我得到了这个返回?

I am trying to create a script that logins to a webpage and grabs a report - all is working GREAT except - I am getting a

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

Response and I have no idea how to stop it. I have tried settings the

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

It's trying me crazy as I can login to the website via https:// but then I get this returned?

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

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

发布评论

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

评论(2

吻风 2024-08-01 08:30:36

我被这个问题困扰了很长时间 - 很高兴我能提供帮助。 阅读这篇文章

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

关键问题是您无法使用启用自动重定向的 HttpWebRequest执行涉及 302 和 cookie 的登录过程,因为 cookie 在整个过程结束之前不会设置。

解决方案是禁用自动重定向并逐步手动实现整个登录过程(获取 302 重定向响应的“Location”标头以及“Set-cookie”标头,并将它们传递给根据需要连续执行步骤)。

您的 cookie 容器需要抓取一路上的所有 cookie 并在最后提交它们。 如果您收到 302 - 您将会头撞墙,想知道为什么您总是停留在登录页面。

I was stuck on this problem for a long time - so glad I can help. Read this 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 enabled to do a login process involving 302s and cookies because the cookies don't get set until the end of the whole process.

The solution is to disable auto redirects and implement the whole login process manually on a step-by-step basis (get the 'Location' header of 302 redirect responses, as well as the 'Set-cookie' header, and pass these down to successive steps as needed).

Your cookie container needs to grab all the cookies along the way and submit them at the end. If you are getting a 302 - you will be hitting your head against the wall wondering why you keep ending up at the login page.

他是夢罘是命 2024-08-01 08:30:36

我知道,这个问题已经很老了,但谷歌指出了这里。 因此,这是 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;
    }
}

然后创建 WebClient 对象 CookieAwareWebClient wc = new CookieAwareWebClient(); 并执行您需要的任何操作。

编辑:也可以通过 HTTP 和 HTTPS 工作。

I know, that question is old, but google points here. So, here are another solution to 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 WebClient object CookieAwareWebClient wc = new CookieAwareWebClient(); and do whatever You need.

Edit: works via HTTP and HTTPS too.

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