Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the right characters (encoding) 1

Status
Not open for further replies.

haddaway

Programmer
Jul 6, 2005
172
SE
I am doing a WebRequest on a Swedish page. In this example The problem is that all the swedish characters are taken away from the stream. I would like to save it all later as UTF8. Why are the swedish characters taken away? Here is my code:

Dim myRequest As HttpWebRequest = Nothing
myRequest = DirectCast(WebRequest.Create(" HttpWebRequest)
myRequest.Method = "GET"
myRequest.ContentType = "application/x- charset=UTF-8"
Dim myHttpWebResponse As HttpWebResponse = DirectCast(myRequest.GetResponse(), HttpWebResponse)

If myRequest.HaveResponse Then
Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream

'Get stream object
Dim streamRead As StreamReader = New StreamReader(streamResponse)

MsgBox(streamRead.ReadToEnd())

' Release the response object resources.
streamRead.Close()
streamResponse.Close()

'Close response
myHttpWebResponse.Close()
End If
 
Have you tried using one of the overloaded constructors of the StreamReader class?

Code:
Dim streamRead As New StreamReader(streamResponse, System.Text.Encoding.Unicode) 'corresponds to UTF-16

- or -

Dim streamRead As New StreamReader(streamResponse, System.Text.Encoding.UTF8) 'corresponds to UTF-8

The default value is 'System.Text.Encoding.Default', which uses the system's current ANSI code page (e.g. your system's code page).

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
I tried that now but it didn't work. If you use the could above you should get <input type=submit value="Google-sökning" name=btnG> somehere in the result. But in my case the "ö" is not there.

I would be really happy if you could confirm this.

thanks
 
I've copied your code, modified it to accept my company proxy but I can't get the ö to appear...

I'm fiddling with settings and stuff and I will let you know if I find something ;)

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Ruffnekk,

you haven't find any interesting? I am depending on you :)
 
I was going to disapoint you, but I found the solution!!

Now, I've been fiddling so much with the code that I hope you can still use it ;)

Code:
    Dim myRequest As HttpWebRequest = Nothing
    myRequest = DirectCast(HttpWebRequest.Create("[URL unfurl="true"]http://www.google.se"),[/URL] HttpWebRequest)
    myRequest.Method = "GET"
    myRequest.ContentType = "application/x-[URL unfurl="true"]www-form-urlencoded;[/URL] charset=UTF8"

    Dim myHttpWebResponse As HttpWebResponse = DirectCast(myRequest.GetResponse(), HttpWebResponse)
    [b]Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("UTF-7")[/b]
    Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream()

    [b]Dim streamRead As StreamReader = New StreamReader(receiveStream, encode)[/b]

    TextBox1.Text = streamRead.ReadToEnd()

    streamRead.Close()
    myHttpWebResponse.GetResponseStream.Close()
    myHttpWebResponse.Close()

I guess all you have to add to your code is the definition of 'Encode' and use it when you declare your streamreader.

I find it very strange that using UTF-7 results in proper displaying of the data though.

Let me know if it worked ok ;)

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Thank you Ruffnekk, that did work, hope it won't be any problem with other languages ;)

Thank you again for your time!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top