httpWebRequest.GetResponse.GetResponseStream decoding

  • Thread starter Thread starter Thomas Jespersen
  • Start date Start date
T

Thomas Jespersen

Hello

This question has been asked many times, but I haven't been able to find a
solution which works.

When I use System.Net.HttpWebRequest.GetResponse.GetResponseStream to read
the response from a posted form, the result is decoded automatically.

How do I specify that GetResponseStream should use "iso-8859-1" when
decoding?

Some background:

My response includes the Danish characters ÆØÅæøå but xml-encoded like this:
øæåØÆÅ. When I read the response using the
following code the response includes questionmarks (?) instead of the Danish
letters.

[CODE START]
Dim encoding As New System.Text.ASCIIEncoding
Dim requestByte As Byte() = encoding.GetBytes("myXMLRequest")

Dim httpWebRequest As System.Net.HttpWebRequest
httpWebRequest = System.Net.HttpWebRequest.Create("http://10.0.0.1")
httpWebRequest.Method = "POST"
httpWebRequest.ContentType = "application/x-www-form-urlencoded"

Dim requestStream As System.IO.Stream = httpWebRequest.GetRequestStream()
requestStream.Write(requestByte, 0, requestByte.Length)
requestStream.Close()

Dim streamReader As New
System.IO.StreamReader(httpWebRequest.GetResponse.GetResponseStream,
System.Text.Encoding.GetEncoding("iso-8859-1"))
Dim responseString As String = streamReader.ReadToEnd()
[CODE END]

This is driving me mad... please help.

Best. regards.

Thomas Jespersen
Mentum, Denmark
MCSE+I, MCSD, MCP+SB, MCT
 
Hello,

I ran the following code on a Windows XP machine with the following
setting. Everything is fine. I suspect it is related to the page you visit.

If possible, please upload a sample, so that I can do some trouble shooting
on my side.


Setting:
---------------------

Regional and Language Options -> Advanced -> Language for non-Unicode
programs -------------> Danish


Code:
---------------------

Sub Main()
Dim myHttpWebRequest As HttpWebRequest =
CType(WebRequest.Create("http://www.microsoft.com/danmark/"),
HttpWebRequest)
Dim myHttpWebResponse As HttpWebResponse =
CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream()
Dim encode As Encoding = System.Text.Encoding.GetEncoding("iso-8859-1")
Dim readStream As New StreamReader(receiveStream, encode)
Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "Response stream
received")
Dim read(256) As [Char]
Dim count As Integer = readStream.Read(read, 0, 256)
Console.WriteLine("HTML..." + ControlChars.Lf + ControlChars.Cr)
Dim s As String
s = ""
While count > 0
Dim str As New [String](read, 0, count)
s = s + str
count = readStream.Read(read, 0, 256)
End While
Console.Write(s)
readStream.Close()
myHttpWebResponse.Close()
Console.ReadLine()
End Sub
 
Hello Parker

Thanks again.

If found the solution by a lucky punch last night. I changed the encoding
from:

System.Text.Encoding.GetEncoding("iso-8859-1") to
System.Text.Encoding.GetEncoding(28591)

28591 is "ISO 8859-1 Latin I". I thought that it was the same, but obviously
not, or maybe GetEncoding is just case sensitive or I should have used "ISO
8859-1 Latin I".

Again... thanks for taking you time, and your generous offer to help me
trouble shoot..

Thomas
 
Back
Top