Accept Cookies in Webclient

How do I add cookies to a webclient?

As Çöđěxěŕ said.

client.Headers.Add(HttpRequestHeader.Cookie,"USERID=HA-49");

This indeed works.

Using CookieContainer with WebClient class

Yes. IMHO, overriding GetWebRequest() is the best solution to WebClient's limited functionalty. Before I knew about this option, I wrote lots of really painful code at the HttpWebRequest layer because WebClient almost, but not quite, did what I needed. Derivation is much easier.

Another option is to use the regular WebClient class, but manually populate the Cookie header before making the request and then pull out the Set-Cookies header on the response. There are helper methods on the CookieContainer class which make creating and parsing these headers easier: CookieContainer.SetCookies() and CookieContainer.GetCookieHeader(), respectively.

I prefer the former approach since it's easier for the caller and requires less repetitive code than the second option. Also, the derivation approach works the same way for multiple extensibility scenarios (e.g. cookies, proxies, etc.).

How can I get the WebClient to use Cookies?

Create a new class the inherits from WebClient that stores the CookieContainer like @Guffa says. Here's code that I use that does that and also keeps the referer alive:

Public Class CookieAwareWebClient
Inherits WebClient

Private cc As New CookieContainer()
Private lastPage As String

Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
Dim R = MyBase.GetWebRequest(address)
If TypeOf R Is HttpWebRequest Then
With DirectCast(R, HttpWebRequest)
.CookieContainer = cc
If Not lastPage Is Nothing Then
.Referer = lastPage
End If
End With
End If
lastPage = address.ToString()
Return R
End Function
End Class

Here's the C# version of the above code:

using System.Net;
class CookieAwareWebClient : WebClient
{
private CookieContainer cc = new CookieContainer();
private string lastPage;

protected override WebRequest GetWebRequest(System.Uri address)
{
WebRequest R = base.GetWebRequest(address);
if (R is HttpWebRequest)
{
HttpWebRequest WR = (HttpWebRequest)R;
WR.CookieContainer = cc;
if (lastPage != null)
{
WR.Referer = lastPage;
}
}
lastPage = address.ToString();
return R;
}
}

Get response cookies after WebClient.UploadStringTaskAsync

I solved this problem by overriding the GetWebResponse(WebRequest request, IAsyncResult result)

public class CustomWebClient : WebClient
{
public CustomWebClient()
{
Cookies = new CookieCollection();
CookieContainer = new CookieContainer();
}

public CookieContainer CookieContainer { get; }
public CookieCollection Cookies { get; private set; }

protected override WebRequest GetWebRequest(Uri address)
{
var request = (HttpWebRequest)base.GetWebRequest(address);

request.CookieContainer = CookieContainer;

return request;
}

protected override WebResponse GetWebResponse(WebRequest request)
{
var response = (HttpWebResponse)base.GetWebResponse(request);

Cookies = response.Cookies;

return response;
}

protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
{
var response = (HttpWebResponse)base.GetWebResponse(request);

Cookies = response.Cookies;

return response;
}
}

Unable to receive cookie using WebClient

Right, so if jsessionid value can be received using the way, provided in question, the solution will be to use HttpWebRequest as ExtendedWebClient somehow ignores the jsession value.

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(new Uri(url, UriKind.Absolute), StoredCookieCollection._CookieCollection);
request.BeginGetResponse(new AsyncCallback(GetSomething), request);

private void GetSomething(IAsyncResult asynchronousResult)
{
// do something
}

// where in url a jsession value is added to the request as &jsessionid=value

The other thing, that also doesn't work in Windows Phone is, that you can't serialize CookieCollection type value so easily. So, I decided to store a String type JSESSIONID value in IsolatedStorage settings instead of it, and create a static class StoredCookieCollection, that will store CookieCollection during the app work.

So, when you receive a jsessionid value, it is saved in settings as string, and saved in static class as CookieCollection :

using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
{
CookieCollection cookies = response.Cookies;
StoredCookieCollection._CookieCollection = cookies;
...
}

That approach will really force the app to work in single session.

Perhaps, there is a more elegant solution with CookieCollection serialization, but I couldn't find it out.



Related Topics



Leave a reply



Submit