Click here to Skip to main content
Rate this:
 
Please Sign up or sign in to vote.
See more: C# .NET
How I Download The WebPage Content Using winInet.dll
Posted 5-Feb-13 1:29am
Comments
Edo Tzumer 5-Feb-13 7:47am
   
If you are using C#.NET why use winInet.dll?
NeerajGSoft 5-Feb-13 10:32am
   
because i worked with httpwebrequest and httpwebresponse classes and it giving me runtime exception "Internal Server Error" at the time of response of url....

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:
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.
  Permalink  
v4
Comments
NeerajGSoft 5-Feb-13 10:30am
   
I tried with webrequest but i'm getting "internal server error" @webresponse object....
sjelen 5-Feb-13 12:37pm
   
You probably didn't make the request right, so the server responded with 'internal server error'.
Maybe you used bad URL, or forgot to authenticate if needed.
The error is not because you used WebRequest class, but because target server could not process your request.

Why don't you try using WebClient.DownloadString method (http://msdn.microsoft.com/en-us/library/fhd1f0sw.aspx)
It will download the page as a string.

Or add a code sample so we can see what you're trying and help you find the problem.
NeerajGSoft 5-Feb-13 22:19pm
   
url="http://links.casemakerlegal.com/states/MS/books/Case_Law/results?search[Cite]=77 So.3d 1094"
Uri urlCheck = new Uri(url);
WebClient wc = new WebClient();
wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = wc.OpenRead(urlCheck);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();


This Is My Code To Make A Request To Server........
sjelen 6-Feb-13 7:18am
   
See the code in updated answer.
NeerajGSoft 6-Feb-13 23:25pm
   
thank u....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Web04 | 2.8.160204.4 | Advertise | Privacy
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100