题
我是比较新的使用C#,并且具有在网站上读取源代码的部件的应用程序。这所有的作品;但问题是,有问题的页面需要登录后才能访问这个源代码的用户。什么我的程序需要一种方式来初次登录用户进入网站 - 做到这一点后,我就可以访问和读取源代码。
这需要被登录到该网站是: mmoinn.com/index.do?PageModule=UsersLogin
我搜索了有关如何做到这一点,并试图例子整个一天,但有没有运气。
在预先感谢
没有正确的解决方案
其他提示
您可以继续使用Web客户端来POST(而不是GET,这是 HTTP动词你“再与目前使用DownloadString),但我想你会发现很容易与(略)下级类工作的WebRequest和WebResponse。
有两个部分这 - 第一个是张贴登录表单,二是回收“的Set-Cookie”报头和发送与您的GET请求沿回服务器为“曲奇”。服务器将使用该Cookie从现在开始(假设它使用基于cookie的身份验证这我相当有信心,这是因为该页面返回一个Set-Cookie头,其中包括“PHPSESSID”)。确定你
<强>张贴到登录表单强>
表的帖子很容易模仿,它只是格式化您的文章数据如下的情况:
field1=value1&field2=value2
使用WebRequest类和代码,我改编自斯科特Hanselman的,这里是你如何POST形式的数据来登录表单:
string formUrl = "http://www.mmoinn.com/index.do?PageModule=UsersAction&Action=UsersLogin"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
string formParams = string.Format("email_address={0}&password={1}", "your email", "your password");
string cookieHeader;
WebRequest req = WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
下面是你应该在Set-Cookie头看看你的登录表单的例子:
PHPSESSID=c4812cffcf2c45e0357a5a93c137642e; path=/; domain=.mmoinn.com,wowmine_referer=directenter; path=/; domain=.mmoinn.com,lang=en; path=/;domain=.mmoinn.com,adt_usertype=other,adt_host=-
<强>获取的页面登录表单后面强>
现在您就可以执行GET请求,你需要先登录一个页面。
string pageSource;
string getUrl = "the url of the page behind the login";
WebRequest getRequest = WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
修改强>
如果您需要查看第一POST的结果,可以恢复它与返回的HTML:
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
放置此正下方cookieHeader = resp.Headers["Set-cookie"];
然后检查在pageSource保持的字符串。
可以通过创建从Web客户端导出,覆盖其GetWebRequest方法并在其上设置的CookieContainer对象的类简化事情很多。如果你总是设置相同的CookieContainer实例,则cookie管理将自动为您处理。
但在HttpWebRequest的获得被发送之前它的唯一的方法是从Web客户端继承和覆盖该方法。
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;
}
return request;
}
}
var client = new CookieAwareWebClient();
client.BaseAddress = @"https://www.site.com/any/base/url/";
var loginData = new NameValueCollection();
loginData.Add("login", "YourLogin");
loginData.Add("password", "YourPassword");
client.UploadValues("login.php", "POST", loginData);
//Now you are logged in and can request pages
string htmlSource = client.DownloadString("index.php");
马修·布林德利的,你的代码工作很好的一些网站,我需要(与登录),但我需要更改为HttpWebRequest
和HttpWebResponse
否则我从远程服务器的 404错误的请求的。此外,我想用您的代码来分享我的解决办法,而且是我试过登录到一个的基于Moodle的网站的,但它没有在你的工作一步“的获取页面登录表单后面”,因为当成功的发布的登录,页眉'Set-Cookie'
没有,尽管其他网站返回任何东西一样。
所以我觉得这其中,我们需要存储的下一个请求饼干,所以我加了这一点。 点击搜索结果 到 “的张贴到登录表单强>” 代码块:
var cookies = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
req.CookieContainer = cookies;
,点击 并以 “的获取页面登录表单后面强>”:
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(resp.Cookies);
getRequest.Headers.Add("Cookie", cookieHeader);
,点击
这样做,让我的登录我在的,并得到了“背后的登录页面”的源代码(网站基于Moodle的),我知道这是一个模糊的使用CookieContainer
和HTTPCookies因为我们可能会要求首先是有一个预先设定的发送请求到服务器之前保存的Cookie。这个工作没有问题,无论如何,但这里有一个良好的信息阅读有关WebRequest
和WebResponse
用示例项目和教程:点击
在.NET 点击检索HTTP内容
如何使用HttpWebRequest和HttpWebResponse在.NET
有时候,它可能有助于切断AllowAutoRedirect
和同时设置登录POST
和页面GET
请求相同的用户代理。
request.UserAgent = userAgent;
request.AllowAutoRedirect = false;