windows mobile 6 通过httpwebrequest 调用web api 接口获取cookie的问题,
报错信息为:此请求需要数据缓冲以便成功进行身份验证或重定向。
首先与调用接口这块,验证时通过了,但是获取response的时候,由于 compact netframework 类库里,封装的 httpwebrequest 和httpwebresponse 都没有将cookie封装进去,所以在 request.GetResponse() 的时候,转换不了,因为接口返回的流里面,是有cookie的。
现在问题就是,怎么通过httpwebrequest 获取这个cookie
下面我贴出主代码。
public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent,
Encoding requestEncoding)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
if (requestEncoding == null)
{
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.Headers.Add("X_REG_CODE", "288a633ccc1");//注册码
request.Headers.Add("X_MACHINE_ID", "a306b7c51254cfc5e22c7ac0702cdf87");//硬件标识号
request.Headers.Add("X_REG_SECRET", "de308301cf381bd4a37a184854035475d4c64946");//系统标为符
request.Headers.Add("X_STORE", "0001");//
request.Headers.Add("X_BAY", "0001-01"); //工位
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
request.ContentType = "application/x-www-form-urlencoded";
//request.ContentType = "multipart/form-data";
request.Headers.Add("Accept-Language", "zh-CN");
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.Accept = "*/*";
request.AllowWriteStreamBuffering = false;
request.KeepAlive = true;
if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
else
{
request.UserAgent = DefaultUserAgent;
}
if (timeout.HasValue)
{
request.Timeout = timeout.Value;
}
//如果需要POST数据
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
i++;
}
byte[] data = requestEncoding.GetBytes(buffer.ToString());
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
HttpWebResponse res;
try
{
//错误就在下面这句,报错 此请求需要数据缓冲以便成功进行身份验证或重定向。
res = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
}
return res;
}
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
CookieContainer cookie = new CookieContainer(); cookie.Add(); request.CookieContainer = cookie;
给request添加Cookies的方法就是上面那样的,至于Cookie里要加什么这就看你的需求来了
我题目里面已经写清楚了 ,compact framework 类库里面对HttpWebRequest的封装太简单,并没有将
CookieContainer 和cookie 封装进去,所以你上面的方法是不行的,注意是windows mobile 开发
不是winform 开发,不过还是谢谢你,你的这个在winform里面可行,在windows mobile里面不行。
Mobile开发很早前有弄过,仔细看了一下你的问题,还是没有明白你碰到的问题是什么,Cookie实际上传递的过程都是在Request和Response的Header里,要发送Cookie,只需要在Request的Header里加上Cookie就可以了,要获取Response的Cookie,读取Response的Header的Set-Cookie就能找到了。
有代码吗?
我的问题是,读取Response的时候会报错
也就是这一步
res = (HttpWebResponse)request.GetResponse();
因为转换不了 Response 所以没有办法获取Set-Cookie
Request的header里面怎么加Cookie?
HttpWebResponse里面没有Cookie这个属性,我怎么获取
@船长他爹:
HttpWebRequest request = WebRequest.Create("http://www.rainier-soft.com/login") as HttpWebRequest; request.Headers.Add("cookie", "user=rainier-soft.com"); var response = request.GetResponse(); string cookie = response.Headers.Get("Set-Cookie"); using (Stream stream = response.GetResponseStream()) { StreamReader reader = new StreamReader(stream); string text = reader.ReadToEnd(); }
@Rainier-Soft.Com: 上面的代码在WM6.5里是可以正常执行的,可以提交cookie,也可以获取到cookie。
@Rainier-Soft.Com:
我的为什么不行,我的设备的系统也是WM6.5,刚按照你的方法试了一下,还是报上面的错误。
我的web api 页面上调用没问题,所以不是接口问题。
还是我这段代码有问题,大哥帮我再看看,费心了哈
@船长他爹: http://bbs.csdn.net/topics/390250352
看看这里的最后一个回复对你有没有用
@Rainier-Soft.Com:
这个我试过,没用0.0
@船长他爹: 那就看看能不能抓包,看返回的数据是什么内容导致抛异常。
@Rainier-Soft.Com:
这段代码 在winform程序里面是可以的,winform里面的.netFrameWork 里面是有封装Cookie类的。
但是VM 6.5的类库 Compact FrameWork里面并没有封装 Cookie。
我觉得错误就在这里
@船长他爹: 上面不是都说过了嘛,Cookie在传递的过程中只是Header里的一项数据,我给你的代码在WM6.5里也实际运行过,提交了Cookie,也能接收Cookie,所以可以确定不是有没有封装Cookie的问题。
@Rainier-Soft.Com:
你给的代码没用,cookie是http协议里面的,compact netFrameWork 类库没有封装这个,缺少cookie这个东西,导致着不能强制转换
(HttpWebResponse)request.GetResponse();
你这段代码要是放在winform程序里面,铁定没什么问题,但是WM里面不行
@船长他爹: 不是都跟你说了嘛,这段代码我是在WM6.5的环境里运行通过了的,可以在Request.Header里提交Cookie,可以在Response.Header里拿到服务器返回的Cookie,所以你的问题可能不是Cookie的问题。
@Rainier-Soft.Com: 那问题出在哪里?
要不你执行以下我的这段代码试试?
@Rainier-Soft.Com:
难道是没有身份验证?