C# Cookie已存储,但不能用于登录。错误:登录会话已过期,您必须重新登录

C# Cookie已存储,但不能用于登录。错误:登录会话已过期,您必须重新登录,c#,.net,wpf,session-cookies,C#,.net,Wpf,Session Cookies,我是c#的新手,当我对uri调用api时,它要求我在登录uri后存储cookie。我使用静态变量来存储它。但仍然无法登录。它仍然显示错误: 会话登录已过期,当我打印出来时,您必须再次登录 cookies,2请求时的相同cookie,登录uri,例如:JSESSION: 512F582H395D465BAFDF63B0D5D21C16 有人能帮我查一下吗?多谢各位 static Uri uri = new Uri("http://myurl.com/login"

我是c#的新手,当我对uri调用api时,它要求我在登录uri后存储cookie。我使用静态变量来存储它。但仍然无法登录。它仍然显示错误:

会话登录已过期,当我打印出来时,您必须再次登录 cookies,2请求时的相同cookie,登录uri,例如:JSESSION: 512F582H395D465BAFDF63B0D5D21C16

有人能帮我查一下吗?多谢各位

        static Uri uri = new Uri("http://myurl.com/login"); //example uri
        static Uri uri1 = new Uri("http://myurl.com/listMap");

        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            await Login_ClickAsync();

            foreach (Cookie cookie in cookieContainer.GetCookies(uri))
            {
                Console.WriteLine("Cookie0:" + cookie.ToString());
            }
        }

        static CookieContainer cookieContainer = new CookieContainer();


        static HttpClientHandler httpClientHandler = new HttpClientHandler
        {

            CookieContainer = cookieContainer
        };

        static HttpClient client = new HttpClient(httpClientHandler);
        static async Task Login_ClickAsync()
        {


            client.DefaultRequestHeaders
                          .Accept
                          .Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var keyValues = new List<KeyValuePair<string, string>>();
            // add API method parameters
            keyValues.Add(new KeyValuePair<string, string>("userName", "testaccount"));
            keyValues.Add(new KeyValuePair<string, string>("password", "123456"));
            HttpContent multiForm = new FormUrlEncodedContent(keyValues);

            //request.ContentType = "application/x-www-form-urlencoded";
            HttpResponseMessage resposne = await client.PostAsync(uri, multiForm);

            //CookieContainer _cookieContainer = 

            cookieContainer = GetCookies(resposne); ;

            if (resposne.IsSuccessStatusCode)
            {


                foreach (Cookie cookie in cookieContainer.GetCookies(uri))
                {
                    Console.WriteLine("Cookie1:" + cookie.ToString());
                }

                await getData();
            }
            else
            {
                Console.WriteLine("False");
            }

        }
        public static CookieContainer GetCookies(HttpResponseMessage response)
        {
            var pageUri = response.RequestMessage.RequestUri;
            var _cookieContainer = new CookieContainer();
            IEnumerable<Cookie> responseCookies = cookieContainer.GetCookies(uri).Cast<Cookie>();
            foreach (Cookie c in responseCookies)
            {
                Console.WriteLine("Coookie111:" + c);
                _cookieContainer.Add(pageUri, c);
            }
            return _cookieContainer;
        }
        static async Task getData()
        {


            var keyValues = new List<KeyValuePair<string, string>>();
            HttpContent multiForm = new FormUrlEncodedContent(keyValues);

            HttpResponseMessage resposne = await client.PostAsync(uri1, multiForm);

            if (resposne.IsSuccessStatusCode)
            {
                string response1 = await resposne.Content.ReadAsStringAsync();
                Console.WriteLine(response1.ToString());


                foreach (Cookie cookie in cookieContainer.GetCookies(uri1))
                {
                    Console.WriteLine("Cookie2:" + cookie);
                }
            }
            else
            {
                Console.WriteLine("False");
            }

        }
    }```
静态Uri=新Uri(“http://myurl.com/login"); //示例uri
静态Uri uri1=新Uri(“http://myurl.com/listMap");
静态异步任务主(字符串[]args)
{
控制台。WriteLine(“你好,世界!”);
等待登录\u单击异步();
foreach(cookieContainer.GetCookies(uri)中的Cookie)
{
Console.WriteLine(“Cookie0:+cookie.ToString());
}
}
静态CookieContainer CookieContainer=新CookieContainer();
静态HttpClientHandler HttpClientHandler=新HttpClientHandler
{
CookieContainer=CookieContainer
};
静态HttpClient客户端=新HttpClient(httpClientHandler);
静态异步任务登录\u ClickAsync()
{
client.DefaultRequestHeaders
接受
.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
var keyValues=新列表();
//添加API方法参数
添加(新的KeyValuePair(“用户名”、“测试帐户”);
添加(新的KeyValuePair(“密码”,“123456”);
HttpContent multiForm=新格式URLEncodedContent(keyValues);
//request.ContentType=“application/x-www-form-urlencoded”;
HttpResponseMessage resposne=wait client.PostAsync(uri,多种格式);
//CookieContainer\u CookieContainer=
cookieContainer=GetCookies(resposne);
if(响应IsSuccessStatusCode)
{
foreach(cookieContainer.GetCookies(uri)中的Cookie)
{
Console.WriteLine(“Cookie1:+cookie.ToString());
}
等待getData();
}
其他的
{
控制台。写入线(“假”);
}
}
公共静态CookieContainer GetCookies(HttpResponseMessage响应)
{
var pageUri=response.RequestMessage.RequestUri;
var_cookieContainer=新cookieContainer();
IEnumerable responseCookies=cookieContainer.GetCookies(uri.Cast();
foreach(响应Cookie中的Cookie c)
{
控制台写入线(“Coookie111:+c”);
_添加(pageUri,c);
}
返回cookieContainer;
}
静态异步任务getData()
{
var keyValues=新列表();
HttpContent multiForm=新格式URLEncodedContent(keyValues);
HttpResponseMessage resposne=等待客户端.PostAsync(uri1,多种形式);
if(响应IsSuccessStatusCode)
{
字符串response1=wait resposne.Content.ReadAsStringAsync();
Console.WriteLine(response1.ToString());
foreach(cookieContainer.GetCookies(uri1)中的Cookie Cookie)
{
Console.WriteLine(“Cookie2:+cookie”);
}
}
其他的
{
控制台。写入线(“假”);
}
}
}```

在尝试回答之前,先问一个简单的问题,RestSharp是否适合您的程序?用于从c#调用api。如果可能,请检查此链接我如何通过应用程序/x-www-form-urlencoded传递请求正文@BijuKalanjoorThats可能与Restsharp合作,请检查共享文档,这里绝对可能也提到了不同类型的请求parameters@BijuKalanjoor哦,谢谢你,兄弟,我试过使用RestSharp,效果很好