c#httpwebrequest发布和获取方法(使用cookieContainer)c#

c#httpwebrequest发布和获取方法(使用cookieContainer)c#,c#,cookiecontainer,C#,Cookiecontainer,我正在尝试登录xbox live页面,但遇到了一些问题,不知道为什么,我想我已正确设置了所有内容。。。这是我的密码 CookieCollection cookies = new CookieCollection(); HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://account.xbox.com/en-US/PaymentAndBilling/RedeemCode"); Request.CookieCont

我正在尝试登录xbox live页面,但遇到了一些问题,不知道为什么,我想我已正确设置了所有内容。。。这是我的密码

CookieCollection cookies = new CookieCollection();
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://account.xbox.com/en-US/PaymentAndBilling/RedeemCode");
Request.CookieContainer = new CookieContainer();
Request.CookieContainer.Add(cookies);
//Request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";

HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
Response.Cookies.Add(cookies);

Response.Close();

HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create("https://login.live.com/");
getRequest.Method = "POST";
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies);

string postData = String.Format("login=/*dd*/&passwd=/*pass*/");
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();

HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
getResponse.Cookies = cookies;
StreamReader sr1 = new StreamReader(getResponse.GetResponseStream());
string sourceCode = sr1.ReadToEnd();
richTextBox1.Text = sourceCode;
sr1.Close();

我真的非常感谢任何帮助,或者任何信息,我可以在哪里找到一些关于cookiecontainers、c#中的http协议等的解释。。。。因为这是我第一个处理WebRequests的程序,所以非常感谢您的帮助。

好吧,您会恨我的,但是如果我有任何选择,我不会使用HttpwebResponse,我会使用


这很容易,因为它使用了完整的浏览器,而不需要维护cookies。如果您需要以交互方式运行它,或者在没有gui的情况下,您可以使用SimpleDriver()

您是否遇到错误?当我将源代码放入浏览器时,它说我应该启用Cookie和javascript。。。但它甚至没有返回我想要的代码(登录后的页面)HttpWebResponse速度更快,我看到一个人通过HttpWebResponse登录这个网站,他说他可以以20美元的价格卖给我这个代码。。。也许我能找到更聪明的人here@Propert是的,速度更快,内存更少,但没有那么简单或强大。我希望看到HttpWebResponse在与网格和应用程序交互时模仿asp请求forms@Propert更不用说填写和解决captacha以及许多其他非常好的项目。这并不是解决他/她的问题的方法。