Click here to Skip to main content
Rate this: bad
good
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 0:29am
Comments
Edo Tzumer at 5-Feb-13 7:47am
   
If you are using C#.NET why use winInet.dll?
NeerajGSoft at 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 at 5-Feb-13 10:30am
   
I tried with webrequest but i'm getting "internal server error" @webresponse object....
sjelen at 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 at 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 at 6-Feb-13 7:18am
   
See the code in updated answer.
NeerajGSoft at 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
0 Sergey Alexandrovich Kryukov 690
1 Dnyaneshwar@Pune 540
2 ravikhoda 325
3 OriginalGriff 279
4 Manas Bhardwaj 230
0 Sergey Alexandrovich Kryukov 10,974
1 OriginalGriff 6,777
2 Peter Leow 4,785
3 Abhinav S 3,813
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid