如何使用c#发送真正的cookie?

时间:2014-10-09 20:28:49

标签: c# cookies

这是通过提琴手获得的cookie的内容。 我刚刚编辑了一些值。

如何联系网络服务器并使用此cookie登录?

__cfduid=d20ed7b400843304;
__gads=ID=778240b8ad7:T=407348:S=ALNI_MatBU2iOvQ3Gb0MAEN-P7fPp-ug;
ChattaMessagesUID=72472;
ChattaMessagesCurrentTalk=;
ChattaMessagesChatNotificationSoundDisabled=;
__utma=37382.34333.4073837.4287477.4288303.;
__utmc=37382;
__utmz=37382.4073837...utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);
__unam=3f2073-488a8e44a-202dc74-84;
ChattaMessagesOpenedChats=33243||0;
ChattaMessagesChatTalkListStartIndex=0;
ChattaMessagesChatActiveTalkId=0;
authCookie=40734003E300A4B7DD434A407;
ASP.NET_SessionId=zprgxvfxgort24ey3jas4

我想我应该使用类似的东西,但我没有找到一个明确的例子......

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("htmysite.com");
req.CookieContainer = cookieContainer;
cookieContainer.SetCookies(new Uri("rrrr.it"), @"_sdsfdsdsf");

1 个答案:

答案 0 :(得分:1)

这样的事情可以完成这项任务(尽管使用HttpClient而不是HttpWebRequest)。只需修改它以满足您的需求。

async Task<HttpResponseMessage> PostAsync(
    Uri uri,
    IEnumerable<KeyValuePair<string, string>> parameters = null,
    IEnumerable<Cookie> cookies = null)
    {
        if (parameters == null)
        {
            parameters = new Dictionary<string, string>();
        }

        using (HttpClientHandler handler = new HttpClientHandler())
        {
            handler.CookieContainer = new CookieContainer();

            if (cookies != null)
            {
                foreach (Cookie cookie in cookies)
                {
                    handler.CookieContainer.Add(cookie);
                }
            }

            using (HttpClient client = new HttpClient(handler))
            {
                using (HttpContent content = new FormUrlEncodedContent(parameters))
                {
                    return await client.PostAsync(uri, content);
                }
            }
        }
    }