I tried to send a cookie to Server. But when it does, it throws a weird exception I can not trace:
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
The line where the exception is thrown:
Request.BeginGetResponse( new AsyncCallback( GetResponseCallback ) , Request );
At the beginning I've never thought it was a cookie issue until I commented out this line:
WCRequest.CookieContainer = WBackgroundTransfer.Cookies;
The exception is gone if the line above is commented. So I tried another approach:
CookieContainer CC = new CookieContainer();
CC.Add( new Cookie( "aa", "bb" ) );
WCRequest.CookieContainer = CC;
The above line does not throw any exception. Then I proceed to modify my code:
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;
And the exception is back!
Then I tried to modify the code again:
WCRequest.Headers[ HttpRequestHeader.Cookie ] = WBackgroundTransfer.Cookies.GetCookieHeader( ReqUri );
The exception still persist. The I modify the code to:
WCRequest.Headers[ HttpRequestHeader.Cookie ] = "ab=cd";
This works!
So I think some of the cookie is causing this issue. I proceed to compare the server response.
The exact cookie I am sending to server is:
PHPSESSID=j0g94fgvum0flhlop5868gjkklc5bh1v
When I send the above cookie to server, the response is:
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
When I send a random, irrelevant cookie, or not give any cookie to the sever:
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
The only difference of the two request is this line:
Set-Cookie: PHPSESSID=j0g94fgvum0flhlop5868gjkklc5bh1v; path=/; HttpOnly
Which tells me to set the cookie where I sent to it.
This is very frustrating. Is this an API bug? Or am I missing something? I cannot modify the sever behaviour so I am not 100% sure this is the cause.
Any ideas?
Update: This only happens in x64 platform! I am starting to think maybe this is a heisengbug...
Update2: No, this happens on all platforms. I just didn't test it thoroughly:(
Update3: This is hopeless. I am going to try switching to socket to see if it helps
Have you checked this solution: https://stackoverflow.com/a/33863365
You can remove the extra cookies you don't need.
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
HttpCookieManager cookieManager = filter.CookieManager;
foreach (HttpCookie cookie in cookieManager.GetCookies(uri))
{
cookieManager.DeleteCookie();
}
If anyone comes into this issue. For me it is magically resolved by clearing the generated libraries inside the project. i.e. Remove the following directories:
obj/*
bin/*
Debug/*
ARM/*
Release/*
x64/*
Update 1: I just noticed that the HttpWebRequest is Setting the Cookies internally the sending them automatically. Was it supposed to do that?
User contributions licensed under CC BY-SA 3.0