从HTTP响应获取Cookie

问题描述:

我有问题从HTTP响应获取Cookie。林肯定,该响应应该有饼干,但我不能看到他们在我的应用程序。从HTTP响应获取Cookie

这里是我的代码:

private static CookieContainer cookies = new CookieContainer(); 
    private static CookieContainer Cookies 
    { 
     get 
     { 
      return cookies; 
     } 
    } 

    public static async Task<HttpStatusCode> SendPostRequest(string url, string postData) 
    { 
     if (url == null) 
      throw new ArgumentNullException("url"); 

     if (postData == null) 
      throw new ArgumentNullException("postData"); 

     HttpStatusCode statusCodeToReturn = HttpStatusCode.Forbidden; 
     HttpWebRequest webRequest = HttpWebRequest.CreateHttp(url); 
     webRequest.Method = "POST"; 
     var cookies = Cookies; 
     webRequest.CookieContainer = cookies; 
     //webRequest.SupportsCookieContainer = true; 
     using (var requestStream = await webRequest.GetRequestStreamAsync()) 
     { 
      var bytes = Encoding.UTF8.GetBytes(postData); 
      requestStream.Write(bytes, 0, bytes.Length); 
     } 

     using (WebResponse response = await webRequest.GetResponseAsync()) 
     { 
      statusCodeToReturn = WebResponseToHTTPStatusCode(response); 
     } 

     return statusCodeToReturn; 
    } 

饼干(使用Wireshark的):

rack.session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRiJFMzg1ZjYxNzIzNzQ4MmY5NmI3NTMw%0AYWMwZmRjNmVmZjMwMDk4OTgzZGUwNjRlNzIzODlmODNjYzE2YmVmMjNlOQ%3D%3D%0A--30d79cd2276c3236de11104852bba4b84bf80f26; path=/; HttpOnly 
+0

可能重复[WP7 Cookies](http://stackoverflow.com/questions/9622296/cannot-get-cookies-in-wp7-using-httpwebrequest) – ry8806

+0

我明白了。问题出在Cookies中。未设置DOMAIN的Cookie在WP7中不受支持。 – Michal

的问题是在返回的cookie,你可以使用这个变量。未设置DOMAIN的Cookie在WP7中不受支持。

+0

那么有没有机会得到它们呢?例如使用WebBrowser。 –

我想你可以创建一个全局变量的cookie.Such保存在你的App.xaml .cs文件,你可以创建这样一个变量:

public CookieContainer GlobalCookie{get;set;} 

,使GloalCookie等于你成功HttpWebR equest CookieContainer。

然后当你调用另一个API.Hope帮助你:)

+0

我有全局变量。在帖子中查看我的代码。 (私人静态CookieContainer饼干)。 – Michal

+0

我不知道你什么时候给全局静态cookie变量值?我在你的上面的代码段看不到它。 – Sedgwickz

+0

看到我的第一篇文章(问题)。前8行代码。无论如何,我解决了它。 – Michal