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 do I add cookies to a webclient?
As Çöđěxěŕ said.
client.Headers.Add(HttpRequestHeader.Cookie,"USERID=HA-49");
This indeed works.
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;
}
}
add cookie to WebClient
You can add cookies to your webClient.
webClient.Headers.Add(HttpRequestHeader.Cookie, "cookies");
webClient.DownloadFile("http://........", "C://2.pdf");
WebClient login with Cookie and download String
I just get an Answer to my question.
Here's a example of my code:
WebClient client = new WebClient();
client.Headers.Add(HttpRequestHeader.Cookie, "cookievalue");
string download = client.DownloadString("url");
output.Text = download;
In the variable cookievalue I only enter the cookiename=cookievalue.
I get the name and the value from Postman, a useful google chrome tool.
I just have to make sure that i can redirect to any login proteccted page with postman and when this is true, then I can use cookiename and cookievalue to login my webclient and see all pages and download sites with downloadString.
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
Using a Class Defined in a C++ Dll in C# Code
Is There a Reason For C#'S Reuse of the Variable in a Foreach
How to Pass an Array into a SQL Server Stored Procedure
How to Use Linq to Select Object With Minimum or Maximum Property Value
How to Get the Application'S Path in a .Net Console Application
Send Values from One Form to Another Form
Sending Email Through Gmail Smtp Server With C#
How to Pass Data (And References) Between Scenes in Unity
Pass List of Checkboxes into View and Pull Out Ienumerable
Deserialize Json Object into Dynamic Object Using Json.Net
How to Find the Method That Called the Current Method
What Are the Differences Between Generics in C# and Java... and Templates in C++