C# 中的 HttpWebRequest 和表单身份验证

发布于 2024-07-15 21:33:23 字数 1161 浏览 3 评论 0原文

我是一名系统人员,目前正在做一个兼职网络开发项目,所以对它还很陌生。 我正在尝试为 www.portapower.com 编写一个 http 客户端。

它适用于网站上发布的某些项目,如果它们符合特定要求,它将打印一条消息。

尝试访问此页面时:

http ://www.portapower.com/getbainfo.php?fclasscode=1&code=CB1831B.40H&fbrand=QUNFUg==

该网站将我重定向到默认注册页面:

http://www.portapower.com/defaregit.php

这是我编码的片段:

CookieContainer myContainer = new CookieContainer();

HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("http://www.portapower.com/" + urlpart);
request.Credentials = new NetworkCredential("****", "******");
request.CookieContainer = myContainer;
request.PreAuthenticate = true;
request.Method = "POST";
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();

Console.WriteLine(response.StatusCode);
Stream resStream = response.GetResponseStream();
Console.WriteLine(resStream.ToString());

我确实有用户名和密码,从浏览器使用时工作正常。 请告诉我这是否是访问经过身份验证的页面的正确方法。

I am a systems guy and currently doing a part time web development project so am pretty new to it. I am trying to write a http client for www.portapower.com.

It will for certain items which are posted on the website and if they match a particular requirement it will print a message.

While trying to access this page:

http://www.portapower.com/getbainfo.php?fclasscode=1&code=CB1831B.40H&fbrand=QUNFUg==

The website redirects me to a default register page:

http://www.portapower.com/defaregit.php

Here is a snippet of what I coded:

CookieContainer myContainer = new CookieContainer();

HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("http://www.portapower.com/" + urlpart);
request.Credentials = new NetworkCredential("****", "******");
request.CookieContainer = myContainer;
request.PreAuthenticate = true;
request.Method = "POST";
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();

Console.WriteLine(response.StatusCode);
Stream resStream = response.GetResponseStream();
Console.WriteLine(resStream.ToString());

I do have the username and password and it works fine when used from a browser. Please tell me if this a correct way to access a authenticated page.

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

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

发布评论

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

评论(4

再可℃爱ぅ一点好了 2024-07-22 21:33:23

这取决于网站如何对用户进行身份验证。 如果他们使用基本身份验证或 Windows 身份验证,则您可以设置 凭据属性 HttpWebRequest 类 添加到用户名/密码/域信息,它应该可以工作。

但是,听起来您必须在网站上输入用户名/密码,这意味着您必须先登录该网站。 查看主页,这是我在处理登录的

元素中找到的内容:

<form name="formlogin" method="post" action="./defalogin.php" >
  <input name="emtext" type="text" id="emtext" size="12">
  <input name="pstext" type="password" id="pstext" size="12">
  <input type="submit" name="Submit" value="Logn in" 
    onClick="return logincheck()" >
</form>

我仅包含相关部分。

鉴于此,您必须首先使用 HttpWebRequest 转到 ./defalogin.php 页面,并 POST emtextpstext< /代码>值。 另外,请确保设置 CookieContainer< /code> 属性CookieContainer。 当对 POST 的调用返回时,它很可能会填充一个 cookie,您必须将其发送回站点。 只需继续将任何后续 HttpWebRequest 实例上的 CookieContainer 属性设置为该 CookieContainer 即可确保传递 cookie。

然后您将转到链接中指示的页面。

值得关注的还有 logincheck javascript 函数,但查看脚本源代码,它没有做任何值得注意的事情。

It depends on how the website is authenticating users. If they are using basic authentication or Windows authentication, then you can set the Credentials property of the HttpWebRequest class to the username/password/domain information and it should work.

However, it sounds like you have to enter the username/password on the site, which means you are going to have to login to the site first. Looking at the main page, this is what I find in the <form> element that handles login:

<form name="formlogin" method="post" action="./defalogin.php" >
  <input name="emtext" type="text" id="emtext" size="12">
  <input name="pstext" type="password" id="pstext" size="12">
  <input type="submit" name="Submit" value="Logn in" 
    onClick="return logincheck()" >
</form>

I've included only the relevant portions.

Given this, you have to go to the ./defalogin.php page first with the HttpWebRequest and POST the emtext and pstext values. Also, make sure you set the CookieContainer property to an instance of CookieContainer. When that call to POST returns, it's more than likely going to be populated with a cookie which you will have to send back to the site. Just keep setting the CookieContainer property on any subsequent HttpWebRequest instances to that CookieContainer to make sure the cookies are passed around.

Then you would go to the page indicated in the link.

Of concern is also the logincheck javascript function, but looking at the script sources, it does nothing of note.

久隐师 2024-07-22 21:33:23

您传递的凭据用于 Windows 身份验证。 您需要使用模拟提交表单的数据来提交帖子数据,然后捕获响应中设置的会话 cookie,并使用该 cookie 来处理将来的请求

看看这个答案,其中包含执行此操作的代码:

使用HttpWebRequest登录页面

The credentials that you are passing is for windows authentication. You need to submit post data with data that mimics the submission of a form then captrue the session cookie set in the response ans use that cookie for future requests

Take a look at this answer which has the code to do this:

Login to the page with HttpWebRequest

写下不归期 2024-07-22 21:33:23

你不能这样做; 您传递的凭据可以与基本身份验证方案一起使用(即,在浏览器中,您会弹出一个用户名/密码对话框。)您必须模拟将数据输入该表单并捕获登录 cookie 并使用它。

You can't do it this way; the credentials you're passing can be used with a basic authentication scheme (i.e. where, in the browser, you get a username/password dialog popping up.) You'll have to simulate the entry of the data into that form and catch the login cookie and use that.

差↓一点笑了 2024-07-22 21:33:23

NetworkCredential 类实际上用于控制常规 Windows 凭据(NTLM、Kerberos 等)。

该站点是一个在 Apache 上运行的 PHP 站点,因此我认为他们没有使用 NTLM 或 kerberos。

您想要做的是将一些表单字段发布到站点,然后保留您返回的 cookie。 确保在后续请求中将 cookie 推送回网站,以便网站知道您已经登录。

The NetworkCredential class is really for controlling regular windows credentials (NTLM, Kerberos, etc).

That site is a PHP site running on Apache, so I don't think they are using NTLM or kerberos.

What you want to do is post some FORM fields to the site, then keep the cookie you get back. Make sure on subsequent requests you push the cookie back to the site so it knows you've already logged in.

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