2
據小提琴手,我希望記錄如何檢索C#中的所有cookie?
Set-Cookie: JSESSIONID=value; Version=1; Domain=.domain.com.mx; Path=/
Set-Cookie: saplb_*=value; Version=1; Path=/
Set-Cookie: PortalAlias=portal; Path=/
Set-Cookie: MYSAPSSO2=value;path=/;domain=.domain.com.mx;HttpOnly
因此之後這些cookies,我剛剛得到這個cookie:
Set-Cookie: PortalAlias=portal; Path=/
我的登錄驗證碼:
string url = "site.com";
string postdata = "user=username&pass=userpass";
byte[] buffer = Encoding.ASCII.GetBytes(postdata);
**// GET cookies from url
getCookies(url)**
// request
HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(Url);
request.CookieContainer = this.cookies;
// post
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = buffer.Length;
using (Stream postdata_stream = request.GetRequestStream())
postdata_stream.Write(buffer, 0, buffer.Length);
// response
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// here, I expect to receive 4 cookies, but I only get 1
foreach (Cookie c in response.Cookies)
{
log("Name:" + c.Name);
log("Value:" + c.Value);
log("");
this.cookies.Add(new Cookie(c.Name, c.Value, c.Path, c.Domain));
}
}
的問題是當我在提琴手中檢查我的程序響應時,有4個餅乾,但不要爲什麼我只能讀一個。
UPDATE
代碼添加餅乾由GET:
private void getCookies(string url)
{
// request
HttpWebRequest request = CreateWebRequestObject(url);
request.CookieContainer = this.cookies; // protected member
request.Method = "GET";
request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 irefox/10.0.2";
// response
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
foreach (Cookie c in response.Cookies)
{
// add cookies to my CookieContainer
this.cookies.Add(new Cookie(c.Name, c.Value, c.Path, c.Domain));
}
}
}
用的getCookies()我有3/4的cookie:
Set-Cookie: JSESSIONID=value; Version=1; Domain=.domain.com.mx; Path=/
Set-Cookie: saplb_*=value; Version=1; Path=/
Set-Cookie: PortalAlias=portal; Path=/
但還是需要一個cookie:
Set-Cookie: MYSAPSSO2=value;path=/;domain=.domain.com.mx;HttpOnly
另外,我比較請求與小提琴手/的WinMerge:
// program request
$Version=1; saplb_*=value; $Path=/; $Version=1; JSESSIONID=value; $Path=/; Domain=.domain.com.mx
Expect: 100-continue
// firefox request
saplb_*=value; JSESSIONID=value
Connection: keep-alive
爲什麼會出現在我的請求「$」字符?