我试图将cookie发送到服务器.但是,当这样做时,它会引发一个我无法跟踪的怪异异常:
Message "The operation identifier is not valid. (Exception from HRESULT: 0x800710DD)" string
StackTrace " at Windows.Web.Http.Filters.HttpBaseProtocolFilter.SendRequestAsync(HttpRequestMessage request)\r\n at System.Net.Http.HttpHandlerToFilter.<SendAsync>d__1.MoveNext()" string
引发异常的行:
Request.BeginGetResponse( new AsyncCallback( GetResponseCallback ) , Request );
一开始,我从来没有想到这是一个cookie问题,直到我注释掉了这一行:
WCRequest.CookieContainer = WBackgroundTransfer.Cookies;
如果上面的行被注释,该异常消失.所以我尝试了另一种方法:
CookieContainer CC = new CookieContainer();
CC.Add( new Cookie( "aa", "bb" ) );
WCRequest.CookieContainer = CC;
上一行不会引发任何异常.然后,我继续修改我的代码:
CookieContainer CC = new CookieContainer();
foreach( Cookie C in WBackgroundTransfer.Cookies.GetCookies( ReqURI ) )
{
CC.Add( new Cookie( C.Name, C.Value, C.Path, C.Domain ) );
}
WCRequest.CookieContainer = CC;
异常又回来了!
然后我尝试再次修改代码:
WCRequest.Headers[ HttpRequestHeader.Cookie ] = WBackgroundTransfer.Cookies.GetCookieHeader( ReqUri );
异常仍然存在.我将代码修改为:
WCRequest.Headers[ HttpRequestHeader.Cookie ] = "ab=cd";
这可行!
因此,我认为某些Cookie导致了此问题.我继续比较服务器响应.
我要发送到服务器的确切cookie是:
PHPSESSID=j0g94fgvum0flhlop5868gjkklc5bh1v
当我将上述cookie发送到服务器时,响应为:
HTTP/1.1 200 OK
Date: Fri, 06 Nov 2015 16:51:13 GMT
Content-Type: application/xml
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=dd4e3badd19f3f67e86fe1431b7fe895a1446828673; expires=Sat, 05-Nov-16 16:51:13 GMT; path=/; domain=.example.com; HttpOnly
Set-Cookie: PHPSESSID=j0g94fgvum0flhlop5868gjkklc5bh1v; path=/; HttpOnly
Vary: Accept-Encoding
Server: nginx
CF-RAY: 24124d464d493439-HKG
当我发送一个随机的,无关的cookie或不向服务器提供任何cookie时:
HTTP/1.1 200 OK
Date: Fri, 06 Nov 2015 16:53:24 GMT
Content-Type: application/xml
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d0a29df98a2984c4f5c4b2ddbc900fde01446828804; expires=Sat, 05-Nov-16 16:53:24 GMT; path=/; domain=.example.com; HttpOnly
Vary: Accept-Encoding
Server: nginx
CF-RAY: 2412507ad4c6330d-HKG
这两个请求的唯一区别是此行:
Set-Cookie: PHPSESSID=j0g94fgvum0flhlop5868gjkklc5bh1v; path=/; HttpOnly
这告诉我设置发送到它的cookie.
这非常令人沮丧.这是API错误吗?还是我错过了什么?我无法修改服务器行为,因此我不是100%确定这是原因.
有任何想法吗?
更新:这仅在x64平台上发生!我开始认为也许这是一个海森伯…
Update2:不,这在所有平台上都会发生.我只是没有彻底测试:
Update3:这是没有希望的.我将尝试切换到套接字以查看是否有帮助
解决方法:
您是否已检查以下解决方案:https://*.com/a/33863365
您可以删除不需要的多余Cookie.
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
HttpCookieManager cookieManager = filter.CookieManager;
foreach (HttpCookie cookie in cookieManager.GetCookies(uri))
{
cookieManager.DeleteCookie();
}