Results 1 to 3 of 3

Thread: [RESOLVED] WebClient.DownloadString problem.

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Resolved [RESOLVED] WebClient.DownloadString problem.

    This doesn't work. Sometimes I get an empty string while at other times I get an error saying connection was closed by remote client.
    The url opens OK in all web browsers.
    Anyone knows why?

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wc As New System.Net.WebClient
        Dim s As String = wc.DownloadString("http://www.yellowpages.com.au")
        TextBox1.Text = s
    End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: WebClient.DownloadString problem.

    I just tried and got the same result. I don't know the answer but I'm going to take a guess and say that the web server is checking the user agent and only responding to "genuine" browsers. You might have to add a header to your WebClient that it recognises as a legitimate user agent.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: WebClient.DownloadString problem.

    Looks like for some reason only the .au site sends gzip encoded data.

    This worked for me.

    Code:
    Imports System.Net
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim wc As New WebClientEx()
            TextBox1.Text = wc.DownloadString("http://www.yellowpages.com.au")
        End Sub
    
    End Class
    
    Public Class WebClientEx
        Inherits WebClient
    
        Protected Overrides Function GetWebRequest(address As System.Uri) As System.Net.WebRequest
            Dim req = CType(MyBase.GetWebRequest(address), HttpWebRequest)
            req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
            Return req
        End Function
    
    End Class
    *Edit*

    If you'd rather not create your own class to extend the WebClient here's how you'd go about it.

    Code:
            Dim request = CType(WebRequest.Create("http://www.yellowpages.com.au"), HttpWebRequest)
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)"
            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
            request.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
            Using response = request.GetResponse()
                Using responseStream = response.GetResponseStream()
                    Using sr = New IO.StreamReader(responseStream)
                        TextBox1.Text = sr.ReadToEnd()
                    End Using
                End Using
            End Using
    Last edited by MattP; Dec 12th, 2011 at 11:30 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width