Click here to Skip to main content
15,908,581 members

Response to: How To Use InternetOpen, InternetOpenUrl and InternetReadFile

Latest Revision
In c# you do not need to use winInet.dll directly, there are several .NET classes you can use:
WebClient[^] is a wrapper around wininet and probably easiest to use.

WebRequest[^] and it's descendents will give you more control over creating requests and processing response.

In your case problem seems to be that you need to handle cookies so the server you're connecting to can use session.
WebClient class does not handle cookies by default but you can extend it:
C#
public class WebClientEx : WebClient
{
    private readonly CookieContainer _Container = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        HttpWebRequest webRequest = request as HttpWebRequest;
        if (webRequest != null)
        {
            webRequest.CookieContainer = _Container;
        }
        return request;
    }
}

and use WebClientEx instead of WebClient.
Posted 5-Feb-13 1:19am by sjelen.
Tags: ,