2012-02-29 30 views
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 

爲什麼會出現在我的請求「$」字符?

回答

1

使用Fiddler將您從代碼製作的HTTP請求與瀏覽器製作的代碼進行比較。

爲了做到這一點,選擇兩個請求,然後按CTRL + W¯¯

而且(你可能必須配置由以下these instructions比較工具),試圖檢查從瀏覽器發起的所有請求。有可能在之前的請求中收到一些cookie(通常是您在請求登錄頁面時發出的GET請求)。如果需要,請首先執行GET,收集Cookie,然後執行POST。