Reputation: 49
I've made short program with 6 textbox that I manually fill in with cookies. Now I want my program to act on specific url as if its logged in.
How can I get that ? I tried this and I get http response that I'm not logged in.
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();
Upvotes: 2
Views: 4769
Reputation: 35477
You need to set the cookie container of the HTTP request. Add the following line after you create the HtppWebRequest
.
myReq.CookieContainer = gaCookies
Upvotes: 1