gpt4 book ai didi

C# Metro HttpClient 在 PostAsync 上没有收到 cookie

转载 作者:太空狗 更新时间:2023-10-30 00:43:18 25 4
gpt4 key购买 nike

我正在尝试使用 .NET 4.5 HttpClient 登录网站并接收 cookie。我在离开尝试之前中断并检查 CookieContainer 并且它不包含 cookie。尽管响应发回了 200 状态。

private async void Login(string username, string password)
{
try
{
Uri address = new Uri(@"http://website.com/login.php");
CookieContainer cookieJar = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler()
{
CookieContainer = cookieJar
};
handler.UseCookies = true;
handler.UseDefaultCredentials = false;
HttpClient client = new HttpClient(handler as HttpMessageHandler)
{
BaseAddress = address
};

HttpContent content = new StringContent(string.Format("username={0}&password={1}&login=Login&keeplogged=1", username, password));
HttpResponseMessage response = await client.PostAsync(client.BaseAddress, content);
}

我不知道为什么这不起作用。当我尝试 .NET 4 风格时它工作正常。

最佳答案

使用FormUrlEncodedContent而不是 StringContentstring.Format .您的代码未正确转义用户名和密码。

HttpContent content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password),
new KeyValuePair<string, string>("login", "Login"),
new KeyValuePair<string, string>("keeplogged", "1")
});

关于C# Metro HttpClient 在 PostAsync 上没有收到 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11660743/

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