.NET HTTP POST 方法 - Cookie 问题 [英] .NET HTTP POST Method - Cookies issue

查看:25
本文介绍了.NET HTTP POST 方法 - Cookie 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C# 登录到 hotfile.com.第一个大问题是克服 417 错误,这行代码解决了它:

I'm trying to use C# to login to hotfile.com. The first big issue was to overcome the 417 Error, which this line solved it:

System.Net.ServicePointManager.Expect100Continue = false;

现在我在尝试使用 POST 登录时收到此错误:

Now I'm getting this error as I try to login using POST:

您似乎不接受 cookie.需要 Cookie 才能登录.帮助

You don't seem to accept cookies. Cookies are required in order to log in. Help

我试了好几次,用谷歌搜索,我仍然无法登录到 Hotfile.com.. 我的代码是这样的:

I've tried several times, and googled around and I still can't login to Hotfile.com.. My code is this:

string response;
byte[] buffer = Encoding.ASCII.GetBytes("user=XX&pass=XX");

CookieContainer cookies = new CookieContainer();
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://hotfile.com/login.php");
WebReq.Credentials = new NetworkCredential("XX", "XX");
WebReq.PreAuthenticate = true;
WebReq.Pipelined = true;
WebReq.CookieContainer = cookies;
WebReq.KeepAlive = true;
WebReq.Method = "POST";
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
WebReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1)";

Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();

HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
response = _Answer.ReadToEnd();
File.WriteAllText("dump.html", response);

自然,用户和通行证会有您的帐户值.

Naturally, the user and pass would have your accounts values.

推荐答案

这是我为你写的一个工作示例:

Here's a working example I wrote for you:

var cookies = new CookieContainer();
ServicePointManager.Expect100Continue = false;

var request = (HttpWebRequest)WebRequest.Create("http://www.hotfile.com/login.php");
request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var requestStream = request.GetRequestStream())
using (var writer = new StreamWriter(requestStream))
{
    writer.Write("user=XX&pass=XX&returnto=/");
}

using (var responseStream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(responseStream))
{
    var result = reader.ReadToEnd();
    Console.WriteLine(result);
}

这篇关于.NET HTTP POST 方法 - Cookie 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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