读取包含0x00个字符的网页时出现问题。响应内容被截断

编程入门 行业动态 更新时间:2024-10-15 10:16:56
本文介绍了读取包含0x00个字符的网页时出现问题。响应内容被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,

我编写了一个下载网页的程序。它适用于大多数网页,但我找到了一些不起作用的页面。

I write a program wich download web pages. It works fine for most of web pages but i have found some pages where it doesn't work.

这些页面包含0x00个字符。

These pages contains 0x00 characters.

I能够阅读页面内容直到这个角色,而不是之后的内容。响应流将0x00字节视为流的末尾。

I'm able to read page content until this character, but not the content after. The response stream consider the 0x00 byte as the end of the stream.

我使用这部分代码来读取响应:

I use this part of code to read the response :

IAsyncResult ar = null;  HttpWebResponse resp = null;  Stream responseStream = null;  String content = null;  ...  resp = (HttpWebResponse)req.EndGetResponse(ar);  responseStream = resp.GetResponseStream();  StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);  content = sr.ReadToEnd(); 

在这个例子中,我使用异步请求,但我尝试使用同步问题,我也有同样的问题。

In this example i use asynchronous request, but i try with synchronous one and i have the same probleme.

我也尝试使用相同的结果:

I also try this with the same result :

HttpWebResponse resp = null;  Stream responseStream = null;  String content = new String();  ...  responseStream = resp.GetResponseStream();  byte[] buffer = new byte[4096];  int bytesRead = 1;  while (bytesRead > 0)  {      bytesRead = responseStream.Read(buffer, 0, 4096);      content += Encoding.UTF8.GetString(buffer, 0, bytesRead);  } 

例如,此网址出现此问题 www.daz3d/i/search/searchsub?sstring=ps% 5Ftx1662b&%5Fm = dps%5Ftx1662b

for example, the problem occurs for this url www.daz3d/i/search/searchsub?sstring=ps%5Ftx1662b&%5Fm=dps%5Ftx1662b

感谢您的回复

Euyeusu PS:问题与类WebClient相同

EuyeusuPS : The problem is the same with class WebClient

推荐答案

字符串变量可以包含0x00字符。所以你需要使用BinaryReader或Stream.Read方法。如果您尝试在字符串变量中分配,那么它将被截断。如果要将内容保存到文件,则保存字节数组而不是字符串。 (File.WriteBytes()) 希望它能解决你的问题

String variable can contain 0x00 character. So you need to use BinaryReader or Stream.Read method. If you try to assign in string variable then it will get truncated. If you are saving the content to a file then save bytes array instead of string.  (File.WriteBytes())Hope it could solve your problem

更多推荐

读取包含0x00个字符的网页时出现问题。响应内容被截断

本文发布于:2023-10-27 10:28:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533101.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网页时   字符   内容

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!