httpwebrequest POST和GET方法(使用cookicontainer)

本文关键字:使用 cookicontainer 方法 POST GET httpwebrequest | 更新日期: 2023-09-27 17:51:20

我正试图登录一个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();

我真的很感激任何帮助,或任何信息在哪里我可以找到一些解释的cookie容器,在c#等http协议....因为这是我第一个使用WebRequests的程序,所以提前感谢您的帮助。

httpwebrequest POST和GET方法(使用cookicontainer)

好吧,你会讨厌我的,但如果我有选择的话,我不会用HttpwebResponse,我会用http://www.seleniumhq.org/projects/webdriver/

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