HttpRequest:通过AuthLogin

发布于 2024-08-20 09:08:19 字数 166 浏览 1 评论 0原文

我需要制作一个简单的程序,使用给定的凭据记录到某个网站,然后导航到某个元素(链接)。 甚至有可能(我的意思是这个 Authlogin 的事情)?

编辑:抱歉 - 我在公司机器上,无法单击“投票”或“添加评论” - 页面显示“已完成,但页面上有错误”(即..)。我非常感谢您的回答和评论,您对我帮助很大!

I would need to make a simple program that logs with given credentials to certain website and then navigate to some element (link).
It is even possible (I mean this Authlogin thing)?

EDIT: SORRY - I am on my company machine and I cannot click on "Vote" or "Add comment" - the page says "Done, but with errors on page" (IE..). I do appreciate your answers and comments, you have helped me a lot!

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

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

发布评论

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

评论(3

勿忘初心 2024-08-27 09:08:19

主要要做的事情是:

  1. 开始使用 Fiddler 来查看需要发送的内容以及以何种方式
  2. 假设我们'就普通的 Web 表单而言,您可能需要将 CookieContainer 与您的 WebRequest 一起使用,以便接受来自登录请求的 cookie,然后重新提供发送后续请求时使用它们(这样的上下文不会由 HttpWebRequest 自动维护):-

    CookieContainer _cookieContainer = new CookieContainer();
    ((HttpWebRequest)请求).CookieContainer = _cookieContainer;
    

Main things to do are:

  1. Start using Fiddler to see what needs to be sent and in what way
  2. Assuming we're talking a normal web form you'll probably need to use a CookieContainer with your WebRequests in order to accept the cookies that come from the login request and then re-supply them when sending subsequent requests (such context is not automagically maintained by HttpWebRequest) :-

    CookieContainer _cookieContainer = new CookieContainer();
    ((HttpWebRequest)request).CookieContainer = _cookieContainer;
    
空城旧梦 2024-08-27 09:08:19

是的。这是可能的。

请参阅以下代码:

        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
        req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
        req.Credentials = new NetworkCredential("admin", "admin");
        req.PreAuthenticate = true;

yes. it is possible.

see following code:

        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
        req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
        req.Credentials = new NetworkCredential("admin", "admin");
        req.PreAuthenticate = true;
时光倒影 2024-08-27 09:08:19

这部分取决于登录过程的管理方式。这实际上是通过网络表单完成的吗?如果是这样,您需要像普通浏览器一样发布表单。

如果是通过 HTTP 身份验证完成的,您应该能够在 Web 请求中设置凭据,告诉它进行预身份验证,一切都会顺利。

It will partly depend on how the login process is managed. Is this actually done via a web form? If so, you'll need to post the form, just as a normal browser would.

If it's done over HTTP authentication, you should be able to set the credentials in the web request, tell it to pre-authenticate, and all should be well.

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