0

我需要在我的 C# 代码中的什么位置放置设置 cookie 的代码?当我登录系统时,Firefox 显示三个 cookie。

登录页面

system.local/interface/Login.php

POST /interface/Login.php HTTP/1.1
Host: system.local
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://system.local/interface/Login.php
Cookie: language=en_US.UTF-8; StationID=06ae3ed4d72a33dd951572df84a13725
Content-Type: application/x-www-form-urlencoded
Content-Length: 71
user_name=user&password=password123456&language=en&action%3Asubmit=Submit

登录时从 index.php 获取响应

http://system.local/interface/index.php

GET /interface/index.php HTTP/1.1
Host: system.local
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: system.local/interface/Login.php  
Cookie: language=en_US.UTF-8; StationID=06ae3ed4d72a33dd951572df84a13725; SessionID=3783a8fea972fb99779f7dd3078ed53c

将 cookie 放在此代码中的什么位置?

string url;
string cookieHeader;
string formparam;
HttpWebRequest request;
HttpWebResponse response;
byte[] bytes;

url = "http://system.local/interface/Login.php";
formparam = string.Format("user_name=" + textBox1.Text + "&password=" + textBox2.Text + "&language=en&action%3Asubmit=Submit");
request = (HttpWebRequest)HttpWebRequest.Create(url);
var cookies = new CookieContainer();
request.ContentType = "Content-Type: application/x-www-form-urlencoded";
request.Method = "POST";
bytes = Encoding.ASCII.GetBytes(formparam);
request.ContentLength = bytes.Length;
request.KeepAlive = true;

using (Stream os = request.GetRequestStream())
{
    os.Write(bytes, 0, bytes.Length);
}

response = (HttpWebResponse)request.GetResponse();
cookieHeader = response.Headers["Set-Cookie"];


request = (HttpWebRequest)WebRequest.Create("http://system.local/interface/Login.php");
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(response.Cookies);
request.Headers.Add("Cookie", cookieHeader);

response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    //richTextBox1.Text = reader.ReadToEnd().Trim();
}
4

2 回答 2

1

在 every 上设置该CookieContainer字段request,它应该会自动从标题中添加 cookie。CookieContainer每次都使用同一个实例,它将在多个请求中保持其状态。您不需要弄乱Cookieset-cookie标头值,因为CookieContainer如果将其分配给请求 cookie 容器,则会自动处理。

var cookies = new CookieContainer();

request.CookieContainer = cookies;
...
response = (HttpWebResponse)request.GetResponse();
// no need to manually read Cookie header, CookieContainer handles that.

request = ...
request.CookieContainer = cookies;
// no need to manually set/populate CookieContainer, just use the same instance.
...
response = (HttpWebResponse)request.GetResponse();
// etc
于 2012-07-31T13:00:54.830 回答
0

我现在解决了我的问题。
我刚写了新的,它的工作很奇怪。

于 2012-08-01T09:17:01.027 回答