gpt4 book ai didi

c# - WebException.Response.GetResponseStream() 限制为 65536 个字符

转载 作者:太空狗 更新时间:2023-10-29 21:30:33 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com