【问题标题】:Cookies not set with RestSharp in Windows Phone 7 appWindows Phone 7 应用程序中未使用 RestSharp 设置 Cookie
【发布时间】:2013-08-24 20:35:15
【问题描述】:

今天我尝试登录一个包含身份验证表单的页面。

我在 Windows Phone 7 上使用 RestSharp,我几乎尝试了所有方法,但没有成功。

在我台式电脑的浏览器中,当我使用登录页面 (http://myanimelist.net/login.php) 并输入我的有效凭据时,我将被重定向到面板页面 (http://myanimelist.net/panel.php)

在 Windows Phone 7(和 8)上,当我尝试使用 RestSharp 进行身份验证时,我被重定向到面板页面,但出现以下错误:

错误:您必须先登录才能看到此页面。

事实上,我没有通过身份验证,也无权查看面板页面。

我在 WPF 应用程序中尝试了同样的事情,并且成功了。

在我的 WPF 应用程序中,我有以下代码:

var client = new RestSharp.RestClient();

client.BaseUrl = "http://myanimelist.net/login.php";

client.Authenticator = new SimpleAuthenticator("username", "mylogin", "password", "mypassword");

client.CookieContainer = new CookieContainer();

var request = new RestRequest(Method.POST);

var response = client.Execute(request);

属性“response.Content”将包含包含一些信息的页面和带有我登录名的欢迎消息。这意味着我已通过身份验证。

但是……

使用 Windows Phone 7 中的以下代码,属性“response.Content”将包含一个页面,其中包含一些信息和以下消息:

错误:您必须先登录才能看到此页面

这是使用的 WP7 代码:

var client = new RestSharp.RestClient();

client.BaseUrl = "http://myanimelist.net/login.php";

client.Authenticator = new SimpleAuthenticator("username", "mylogin", "password", "mypassword");

client.CookieContainer = new CookieContainer();

var myRequest = new RestRequest(Method.POST);


client.ExecuteAsync(myRequest, response =>
{
    var t = response.Content;
});

我错过了什么吗? WP7 上的 RestSharp 和 WPF 应用程序有区别吗?

我使用 Fiddler 检查发生了什么,我可以看到 WP7 上从未设置过 cookie(在 WPF 应用程序中设置了 cookie)。

编辑:

我发现了一些有趣的东西,当我收到响应时,我可以在 Fiddler 中看到在 WPF 应用程序和 WP7 应用程序中设置了一个 cookie。

所以我尝试在我的 WP7 应用程序中添加 cookie 以提出我的请求并且它起作用了。

我添加了在 Fiddler 中可以看到的值,但我找不到使用 RestSharp 检索这些值的方法。

这是我在执行请求之前尝试添加的内容:

myRequest.AddParameter("A", "theFirstValue", ParameterType.Cookie);
myRequest.AddParameter("B", "theSecondValue", ParameterType.Cookie);

【问题讨论】:

    标签: windows-phone-7 windows-phone-8 restsharp


    【解决方案1】:

    其实我找到了办法。

    在响应中,我在 Header (Set-cookie) 中有 cookie。我从标头中提取 cookie 并将其添加到需要 cookie 的下一个请求中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-06
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多