gpt4 book ai didi

c# - 如何在 C# 中检索所有 cookie?

转载 作者:行者123 更新时间:2023-11-30 18:01:03 27 4
gpt4 key购买 nike

根据 fiddler,我希望这些 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

因此,我只得到这个 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));
}
}

问题是,当我在 fiddler 中检查我的程序响应时,有 4 个 cookie,但为什么我只能读取一个。

更新

通过 GET 为 cookie 添加的代码:

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

此外,我将请求与 Fiddler/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

为什么我的请求中有一个“$”字符?

最佳答案

使用 Fiddler 比较您从代码发出的 HTTP 请求与从浏览器发出的请求。

为此,选择两个请求并按 CTRL+W(您可能必须按照 these instructions 配置比较工具)

此外,尝试检查从浏览器发起的所有请求。有可能在之前的请求中收到了一些 cookie(通常是您在请求登录页面时发出的 GET 请求)。如果需要,先执行 GET,收集 cookie,然后执行 POST。

关于c# - 如何在 C# 中检索所有 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9505237/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com