无法使用HttpWebRequest在wp7中获取cookie [英] Cannot get cookies in wp7 using HttpWebRequest

查看:204
本文介绍了无法使用HttpWebRequest在wp7中获取cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在发布请求中保存Cookie。这是我的代码:

I'm trying to save cookies in a post request. Here is my code :

        CookieContainer myCookieContainer = new CookieContainer();
        HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
        myHttpWebRequest.UserAgent = userAgent;
        myHttpWebRequest.CookieContainer = myCookieContainer;
        myHttpWebRequest.Method = "POST";

        byte[] postdata = encoding.GetBytes(submitString);

        myHttpWebRequest.BeginGetRequestStream(async1 =>
        {
            using (Stream stream = myHttpWebRequest.EndGetRequestStream(async1))
                stream.Write(postdata, 0, postdata.Length);
            myHttpWebRequest.BeginGetResponse(async2 =>
            {
                HttpWebResponse rep = (HttpWebResponse)myHttpWebRequest.EndGetResponse(async2);
                CookieCollection cookies = rep.Cookies;
                using (Stream stream = rep.GetResponseStream())
                using (StreamReader sr = new StreamReader(stream))
                {
                    String content = sr.ReadToEnd();
                    if (pageDownloadedEventHandler != null)
                        pageDownloadedEventHandler(content);
                }
            }, null);
        }, null);

可以保证CookieContainer为空。
如何获取Cookie?

Alaways the CookieContainer is empty. How to get the cookies?

推荐答案

您的代码似乎是完美的,你应该在rep.Cookies以及myCookieContainer中看到它们。

Your code seems to be perfect, if the server sends you back any cookies you should see them in rep.Cookies, as well as in myCookieContainer.

如果你想确保使用Fiddler或Wireshark来分析HTTP网络流量,饼干,但如果我是对的,你不会找到他们。在这种情况下,我的想法是分析网络流量做与您的浏览器相同的请求,也许由于一些缺少的请求头,php / asp.net /其他应用程序决定不设置Cookie。

If you want to be sure use Fiddler or Wireshark to analyze the HTTP network traffic and look for the cookies, but if I'm right you won't find them. In this case my idea is to analyze the network traffic doing the same request with your browser, maybe the php/asp.net/other app decided not to set cookies due to some missing request headers.

这篇关于无法使用HttpWebRequest在wp7中获取cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆