在 Windows Phone 7 (Mango) 上的 HttpWebRequest 之间共享 CookieContainer 时出现问题
我有一个生产应用程序,它通过 HttpWebRequest 进行两次调用。第一个调用设置会话并接收返回的 cookie 以维持会话,第二个调用用于获取来自 api 的数据。响应是 httponly。我在两个调用之间使用共享的 CookieContainer,但第二个调用总是失败。我将问题范围缩小到第二个请求中未发送 cookie。我使用网络监视器来监视流量,如果我在第二个请求中显式设置 cookie(请参阅下面的代码),则调用会成功。有人对这个问题有什么想法吗?我需要弄清楚如何让它与共享 CookieContainer 一起工作。
private string URL_01 = "https:// [...]";
private string URL_02 = "https:// [...]";
private CookieContainer _cookieContainer = new CookieContainer();
private NetworkCredential nc = new NetworkCredential("username", "password");
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
HttpWebRequest request = HttpWebRequest.CreateHttp(URL_01);
request.CookieContainer = _cookieContainer;
request.Credentials = nc;
request.UseDefaultCredentials = false;
request.BeginGetResponse(new AsyncCallback(HandleResponse), request);
}
public void HandleResponse(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
if (request != null)
{
using (WebResponse response = request.EndGetResponse(result))
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string data = reader.ReadToEnd();
// gets returned data and deserializes it to an object
SessionObject so = JsonConvert.DeserializeObject<SessionObject>(data);
if (so.DeviceAPI.Session == "true")
{
// make a second call for the data
HttpWebRequest requestData = HttpWebRequest.CreateHttp(URL_02);
// when this is used, the call fails
requestData.CookieContainer = _cookieContainer;
// when this is used, the call works
//requestData.Headers[HttpRequestHeader.Cookie] = "_key=value; _secret=value";
requestData.Credentials = nc;
requestData.BeginGetResponse(new AsyncCallback(DataResponse), requestSongData);
}
}
}
}
}
public void DataResponse(IAsyncResult DataResult)
{
HttpWebRequest requestData = DataResult.AsyncState as HttpWebRequest;
if (requestData != null)
{
using (WebResponse dataResponse = requestData.EndGetResponse(DataResult))
{
using (StreamReader reader = new StreamReader(dataResponse.GetResponseStream()))
{
string data = reader.ReadToEnd();
// do something with the data
}
}
}
}
}
}
I have a production app that makes two calls via HttpWebRequest. The first call sets a session and receives cookies back to maintain the session, the second call is for data from the api. The responses are httponly. I am using a shared CookieContainer between the two calls, but the second call always fails. I narrowed the problem down to the cookies not being sent in the second request. I've used Network Monitor to watch the traffic, and if I explicitly set the cookies in the second request (see code below), the call succeeds. Anybody have any ideas on this issue? I need to figure out how to get it to work with the shared CookieContainer.
private string URL_01 = "https:// [...]";
private string URL_02 = "https:// [...]";
private CookieContainer _cookieContainer = new CookieContainer();
private NetworkCredential nc = new NetworkCredential("username", "password");
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
HttpWebRequest request = HttpWebRequest.CreateHttp(URL_01);
request.CookieContainer = _cookieContainer;
request.Credentials = nc;
request.UseDefaultCredentials = false;
request.BeginGetResponse(new AsyncCallback(HandleResponse), request);
}
public void HandleResponse(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
if (request != null)
{
using (WebResponse response = request.EndGetResponse(result))
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string data = reader.ReadToEnd();
// gets returned data and deserializes it to an object
SessionObject so = JsonConvert.DeserializeObject<SessionObject>(data);
if (so.DeviceAPI.Session == "true")
{
// make a second call for the data
HttpWebRequest requestData = HttpWebRequest.CreateHttp(URL_02);
// when this is used, the call fails
requestData.CookieContainer = _cookieContainer;
// when this is used, the call works
//requestData.Headers[HttpRequestHeader.Cookie] = "_key=value; _secret=value";
requestData.Credentials = nc;
requestData.BeginGetResponse(new AsyncCallback(DataResponse), requestSongData);
}
}
}
}
}
public void DataResponse(IAsyncResult DataResult)
{
HttpWebRequest requestData = DataResult.AsyncState as HttpWebRequest;
if (requestData != null)
{
using (WebResponse dataResponse = requestData.EndGetResponse(DataResult))
{
using (StreamReader reader = new StreamReader(dataResponse.GetResponseStream()))
{
string data = reader.ReadToEnd();
// do something with the data
}
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当域名上没有“www”时,此问题是 CookieContainer 的一个已知问题。 CookieContainer 需要一个“www”,并且不识别该域的现有 cookie。
解决方法是从响应标头中读取 cookie,并将它们添加到任何其他请求中。
曾考虑修复 Mango,但被排除在外。
杰夫
This problem is a known issue with the CookieContainer when the domain name has no "www" on it. The CookieContainer is expecting a "www" and does not identify existing cookies for the domain.
The work around was to read the cookies from the response headers and add them to any other requests.
Was considered for a fix for Mango, but got pushed out.
Jeff
我遇到了他的问题,似乎是使用旧的饼干容器。只需通过迭代旧的 cookie 并创建新的 cookie 来创建新的 cookie。
步骤 1. 创建一个字典来存储 cookie。
步骤 2. 在发送 Web 请求之前将您存储的 Cookie 添加到该请求中。
步骤 3. 保存Cookies
ResponseCallback(...)
{
I had his issue and it seemed to be with using an old cookie container. Just create a new one by Iterating through the old one, and creating new cookies.
Step 1. Create a dictionary to store cookies.
Step 2. Add the cookies you have stored, onto the webrequest before it's sent off.
Step 3. Saving the Cookies
ResponseCallback(...)
{