HttpClient的,并在C#中窗体身份验证 [英] HttpClient and forms authentication in C#

查看:400
本文介绍了HttpClient的,并在C#中窗体身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个系统的家伙,目前在做兼职的Web开发项目,所以是pretty新的给它。我试图写一个www.portapower.com http客户端。

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.

在试图访问该页面:

http://www.portapower.com/getbainfo.php?fclass$c$c=1&$c$c=CB1831B.40H&fbrand=QUNFUg==

该网站重定向我到一个默认的注册页面:

The website redirects me to a default register page:

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

下面是一个片段是我codeD:

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.

推荐答案

这取决于网站是如何验证用户身份。如果他们使用基本身份验证或Windows身份验证,然后就可以设置的 证书属性 =http://msdn.microsoft.com/en-us/library/8y7x3zz2.aspx相对= nofollow的> 的HttpWebRequest 类的用户名/密码/域的信息,它应该工作。

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.

然而,这听起来像您必须输入该网站,这意味着你将不得不登录到该网站首先在用户名/密码。纵观主网页,这就是我发现在<形式为GT; 处理login元素:

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.

鉴于此,你必须去到 ./ defalogin.php 页先用的HttpWebRequest 和POST在 emtext pstext 值。此外,请确保您设置的 的CookieContainer 属性来的 的CookieContainer 。当调用POST的回报,这是极有可能会用一个cookie,你将不得不发回现场填充更多。只要保持上设置任何后续的的HttpWebRequest 实例的的CookieContainer 属性的CookieContainer ,以确保饼干传来传去。

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.

值得关注的是将 logincheck JavaScript函数,但看着剧本来源,它什么都不做笔记。

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

这篇关于HttpClient的,并在C#中窗体身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆