Results 1 to 6 of 6

Thread: WebClient.Encoding and diamond question marks

Threaded View

  1. #5
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: WebClient.Encoding and diamond question marks

    A StreamReader uses UTF-8 encoding by default, so you could try modifying your code slightly:
    vb.net Code:
    1. Dim myString As String ' declared here to broaden its Scope
    2.  
    3. Using myWebClient As New Net.WebClient()
    4.  
    5.     Using myStream As IO.Stream = myWebClient.OpenRead(url)
    6.         Using sr As New IO.StreamReader(myStream, System.Text.Encoding.GetEncoding("ISO-8859-1"))
    7.             myString = sr.ReadToEnd
    8.         End Using ' Stream
    9.     End Using ' StreamReader
    10.  
    11.  
    12. End Using ' WebClient

    The WebClient.Encoding Property is used by the WebClient when it uploads or downloads Strings, so you might also try:

    vb.net Code:
    1. Dim myString As String ' declared here to broaden its Scope
    2.  
    3. Using myWebClient As New Net.WebClient()
    4.     myWebClient.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1")
    5.     mystring = myWebClient.DownloadString(url)
    6.  
    7. End Using ' WebClient
    although it defaults to the system's default encoding, so you could probably get away without explicitly setting the encoding in your particular case. It's best to set it explicitly when you can, though. See the MSDN for more details, and also the Remarks section here.
    Last edited by Inferrd; Nov 15th, 2012 at 01:32 AM. Reason: Noticed a Scoping issue with the way I modified the original code

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