简体   繁体   English

无法读取 httpwebresponse

[英]Can't read httpwebresponse

I am getting a invalidoperation exception when I try to Deserialize a webresponse.当我尝试反序列化 webresponse 时,我收到了一个 invalidoperation 异常。

In this webresponse I get an XML page which send over https.在这个网络响应中,我得到一个通过 https 发送的 XML 页面。 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.我的代码所做的基本上是发送一个 webrequest 来获取登录 cookie,然后发送另一个获取 XML 页面的请求,然后我反序列化该页面。

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.所以我将我的 webresponse 放入 streamreader 并使用 readToEnd 将其转换为字符串。 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).我用 Fiddler 查了一下,发现在我看到内容之前需要对响应进行解码(是的,它确实有我想要的正确响应)。 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.我尝试查看 System.Text.Encoding,但找不到十六进制的任何内容。 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.编辑:我可能必须解码它,因为它是一个 https 响应,而不是因为它是十六进制编码的。

edit1: I have tried HttpUtility.HtmlDecode unfortunately it didnt change the string.编辑 1:我试过HttpUtility.HtmlDecode不幸的是它没有改变字符串。

edit2: example of data i am getting编辑2:我得到的数据示例

{ U r 0 +(g (J ԙ$ k;IomR& I N? / 6 L ^` { 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内容长度:382 内容类型:应用程序/xml

0x1f is a Windows control character. 0x1f 是 Windows 控制字符。 It is not valid XML.它不是有效的 XML。

You need to decode it first using something like HttpUtility.Decode您需要先使用HttpUtility.Decode 之类的东西对其进行解码

So: get your response and read it into a string using a StreamReader then do something like:所以:获取您的响应并使用 StreamReader 将其读入字符串,然后执行以下操作:

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) HttpUtility.HtmlDecode 方法(字符串)

EDIT: Similar question编辑:类似的问题

hexadecimal value 0x1F, is an invalid character. 十六进制值 0x1F,是一个无效字符。 Line 1, position 1 第 1 行,位置 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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