- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 HttpWebRequest 和 HttpWebResponse 从网页中检索 HTML 代码。
response = (HttpWebResponse)request.GetResponse();
...
Stream stream = response.GetResponseStream();
响应对象的 ContentLength 值为 106142。当我查看流对象时,它的长度为 65536。当使用 StreamReader 使用 ReadToEnd() 读取流时,仅返回前 65536 个字符。
如何获取完整代码?
编辑:
使用以下代码段:
catch (WebException ex)
{
errorMessage = errorMessage + ex.Message;
if (ex.Response != null) {
if (ex.Response.ContentLength > 0)
{
using (Stream stream = ex.Response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
string pageOutput = reader.ReadToEnd().Trim();
ex.Response.ContentLength = 106142
ex.Response.GetResponseStream().Length = 65536
stream.Length = 65536
pageOutput.Length = 65534(因为修剪)
是的,代码实际上被截断了。
最佳答案
您可以在System.Net.HttpWebResponse.GetResponseStream() returns truncated body in WebException 中找到该主题的答案。
您必须管理 HttpWebRequest
对象并更改 DefaultMaximumErrorResponseLength
属性。例如:
HttpWebRequest.DefaultMaximumErrorResponseLength = 1048576;
关于c# - WebException.Response.GetResponseStream() 限制为 65536 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4865594/
我正在与来自 .Net 的 Web 服务器通信。 Web 服务器抛出 500 内部服务器错误并写入详细的错误消息。 我正在尝试读取从 Web 异常收到的错误消息,但收到另一个 Web 异常。为什么抛出
我有以下从 Internet 检索网页的 F# 程序: open System.Net [] let main argv = let mutable pageData : byte[] = [
我有以下功能将通过代理获取某些网站的 html 源,它工作正常,除了有时服务器返回 503(服务器不可用)或它永远不会进入 catch 语句的任何其他异常。 在 catch 语句中,该函数应该递归调用
我制作了一些假类,它应该具有与原始类相同的行为。原始类有时会抛出 WebExceptions(使用 StatusCode 作为服务器的响应)。 我想在没有任何联系的情况下重复此行为。那么,我如何使用所
我在我的 Web API 服务(经过 Windows 验证)中使用 WebClient 对我们的公司目录(经过 Windows 验证)进行 HTTP 调用以获取用户的配置文件。请看下面。 public
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 个月前。 Improv
我正在扫描具有多个路径示例的单个 url:http://url.com/path1 到 1000。有时我得到一个 WebException 但在我的 catch block 中,如果我不使用该行,它会
我有以下方法,基本上是使用 Facebook 凭据对用户进行身份验证。出于某种原因,我在尝试处理授权 key (代码)时收到了 WebException。因此,我尝试读取响应流以了解发生了什么,但在读
当我运行下面的测试(NUnit 和 VS2008 SP1)时,我得到以下错误 预期:连接失败 但是是:UnknownError 谁能解释我做错了什么? [Test] public void Seria
我正在开发 Winforms 应用程序并使用 C# 作为开发语言。在一个地方,我需要向 URL 发送请求并接收反馈。 我在 try catch block 中使用我的代码并捕获了 WebExcepti
我有 C# 代码对 OData Web 服务执行 POST,从而将记录插入到数据库中。如果出现异常,比如违反主键,我想捕获并记录错误信息。但是,该消息包含在 XML 中。这是我的异常处理程序: cat
当我在调度程序线程上执行以下代码时,每次我开始使用新的模拟器进行调试时,我的 Windows Phone 应用程序都会抛出 WebException 异常: public MainPage() {
我在 Android 的 Mono 上使用 WebRequests,对于某些服务器,证书验证失败。我已经实现了 ICertificatePolicy(因为从未调用过 ServerCertificate
这是一个让我感到困惑的一般性问题。我认为一旦发出 REST 请求,错误就会通过 WebException 返回。在一种情况下,我得到了一个 HttpRequestException,它不允许我获取 H
如何获取WebException错误中的错误号? try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("
我正在使用一个简单的 Web 客户端从 Web 服务中检索一些 XML,我将其封装在一个简单的 try、catch block (捕获 WebException)中。像下面这样; try
假设我创建并执行了一个 System.Net.FtpWebRequest。 我可以使用 catch (WebException ex) {} 来捕获此请求抛出的任何与网络相关的异常。但是,如果我有一些
我正在尝试连接到 FTP 服务器并下载文件,但遇到了一些问题。 最初我使用的是 WebClient.DownloadFileAsync,对于小文件它工作正常,但对于大文件会到达下载结束但永远不会调用
环境 Windows 8.1 Visual Studio 2017社区 C# WPF应用程序 问题 YoutubeExtractor在下载视频时引发System.Net.WebException。 我
我使用 MVC4 Web API 创建了一个 RESTful 网络服务。如果出现问题,我将抛出 WebException。 throw new WebException("Account not fo
我是一名优秀的程序员,十分优秀!