简体   繁体   中英

Can't read httpwebresponse

I am getting a invalidoperation exception when I try to Deserialize a webresponse.

In this webresponse I get an XML page which send over https. What my code does is basically send a webrequest to get login-cookies, then another request to get an XML page, afterwards i deserialize that page.

The problem occurs when I try to deserialize, it gives me the following error:

There is an error in XML document (1, 1).

First I looked into my webresponse to see if it has proper data. So I put my webresponse into the streamreader and used readToEnd to convert it into string. Like so:

dim myStringResult as String = myStreamReader.ReadToEnd()

It gave me some letters numbers and blocks. So I think the response I get is the problem. I checked it out with Fiddler and saw that the response needed to be decoded before I could see the content(and yes it did have the proper response I wanted). I went a little deeper into the exception and saw it says

hexadecimal value 0x1F, is an invalid character. Line 1, position 1

I tried looking into the System.Text.Encoding, but couldn't find anything for hexadecimal. Can anyone help me with this?

edit: It's possible that I have to decode it because it is a https response and not because it's hexadecimal encoded.

edit1: I have tried HttpUtility.HtmlDecode unfortunately it didnt change the string.

edit2: example of data i am getting

{ U r 0 +(g (J ԙ$ k;IomR& I N? / 6 L ^`

it doesn't show the data right. It suppose to be mostly squars. Every now and then you see

edit: the header

content-disposition: : attachment; filename="document.xml" CachingModuleShouldWork: true Content-Language: en-US Vary: Accept-Encoding,User-Agent

Content-Length: 382 Content-Type: application/xml

0x1f is a Windows control character. It is not valid XML.

You need to decode it first using something like HttpUtility.Decode

So: get your response and read it into a string using a StreamReader then do something like:

StringWriter myWriter = new StringWriter();
         // Decode the encoded string.
         HttpUtility.HtmlDecode(myEncodedString, myWriter);

If you're decoding content read from the web I recommend you to check this:

HttpUtility.HtmlDecode Method (String)

EDIT: Similar question

hexadecimal value 0x1F, is an invalid character. Line 1, position 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM