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

查看:14
本文介绍了无法使用 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);

Alaways CookieContainer 是空的.如何获取cookies?

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

推荐答案

你的代码看起来很完美,如果服务器给你发回任何 cookie,你应该在 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 网络流量并查找 cookie,但如果我是对的,您将找不到它们.在这种情况下,我的想法是分析使用您的浏览器执行相同请求的网络流量,也许 php/asp.net/other 应用程序由于某些请求标头丢失而决定不设置 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天全站免登陆