[英]How to send cookie with HttpWebRequest in c#
我用6個文本框制作了簡短的程序,並手動填寫了cookie。 現在,我希望我的程序對特定的URL進行操作,就好像它已登錄一樣。
我該怎么辦? 我嘗試了此操作,但收到未登錄的http響應。
string url = "myurl string";
Uri target = new Uri(url);
CookieContainer gaCookies = new CookieContainer();
gaCookies.Add(new Cookie("__utma", textBox1.Text) { Domain= target.Host});
gaCookies.Add(new Cookie("__utmb", textBox2.Text) { Domain = target.Host });
gaCookies.Add(new Cookie("__utmc", textBox3.Text) { Domain = target.Host });
gaCookies.Add(new Cookie("__utmz", textBox4.Text) { Domain = target.Host });
gaCookies.Add(new Cookie("cookiename1", textBox5.Text) { Domain = target.Host });
gaCookies.Add(new Cookie("cookiename2", textBox6.Text) { Domain = target.Host });
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
textBox7.Text = readStream.ReadToEnd();
您需要設置HTTP請求的cookie容器。 創建HtppWebRequest
之后,添加以下行。
myReq.CookieContainer = gaCookies
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.