【问题标题】:Not generating a complete response from a HttpWebResponse object in C#未从 C# 中的 HttpWebResponse 对象生成完整响应
【发布时间】:2025-11-26 15:05:02
【问题描述】:

我正在从另一个 aspx 页面创建一个 HttpWebRequest 对象,以将响应流保存到我的数据存储中。我用来创建 HttpWebRequest 对象的 URL 具有查询字符串来呈现正确的输出。当我使用任何旧浏览器浏览页面时,它会正确呈现。当我尝试使用 HttpWebResponse.GetResponseStream() 检索输出流时,它会呈现我的内置错误检查。

为什么它会在浏览器中正确呈现,但不使用 HttpWebRequest 和 HttpWebResponse 对象?

这里是源代码:

目标页面后面的代码:

protected void PageLoad(object sender, EventsArgs e)
{
   string output = string.Empty;

   if(Request.Querystring["a"] != null)
   {
      //generate output
      output = "The query string value is " + Request.QueryString["a"].ToString();
   }
   else
   {
      //generate message indicating the query string variable is missing
      output = "The query string value was not found";
   }

   Response.Write(output);
}

创建 HttpWebRequest 对象的页面背后的代码

string url = "http://www.mysite.com/mypage.aspx?a=1";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url)

//this if statement was missing from original example
if(User.Length > 0)
{
    request.Credentials = new NetworkCredentials("myaccount", "mypassword", "mydomain");
    request.PreAuthenticate = true;
}

request.UserAgent = Request.UserAgent;
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Stream resStream = response.GetResponseStream();  
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(resStream, encode, true, 2000);
int count = readStream.Read(read, 0, read.Length);
string str = Server.HtmlEncode(" ");

while (count > 0)
{
    // Dumps the 256 characters on a string and displays the string to the console.
    string strRead = new string(read, 0, count);
    str = str.Replace(str, str + Server.HtmlEncode(strRead.ToString()));
    count = readStream.Read(read, 0, 256);
}

// return what was found
result = str.ToString();

resStream.Close();
readStream.Close();

更新

@David McEwing - 我正在创建带有完整页面名称的 HttpWebRequest。该页面仍在生成错误输出。我更新了目标页面的代码示例,以准确演示我在做什么。

@Chris Lively - 我没有重定向到错误页面,我生成了一条消息,指示未找到查询字符串值。我更新了源代码示例。

更新 1:

我尝试使用 Fiddler 来跟踪 HttpWebRequest,但它没有显示在 Web 会话历史记录窗口中。我是否在我的源代码中遗漏了一些东西来获得完整的网络请求和响应。

更新 2:

我的示例中没有包含以下代码部分,这是导致问题的罪魁祸首。我正在使用服务帐户而不是导致问题的 AD 帐户设置 HttpWebRequestCredentials 属性。

我更新了我的源代码示例

【问题讨论】:

  • 我只是将这两个代码 sn-ps 复制到一个测试站点,第一个进入 default.aspx,另一个进入调用 default.aspx 的页面 test.aspx。它适用于我在 IIS6 和 ASP.NET 开发服务器下。您是否指向正确的测试版本?
  • 我发现了这个问题。我使用的凭据导致页面无法正确呈现。更新我的问题并发布答案。
  • @David McEwing - 为您提供帮助为您点赞!

标签: c# asp.net httpwebrequest query-string httpwebresponse


【解决方案1】:

您使用的是什么网络服务器?我记得在过去使用 IIS 做某事时,有一个问题是 http://example.com/http://example.com/default.asp 之间的重定向丢弃了查询字符串。

也许运行 Fiddler(或协议嗅探器)并查看是否发生了您未预料到的事情。

还要检查传递完整页面名称是否有效。如果确实如此,那么几乎可以肯定是问题所在。

【讨论】:

  • 我正在使用 IIS 5.1 在本地工作站上进行测试,然后将其部署到 IIS 6.0 服务器。
  • 这是我遇到问题时的配置。尝试传递完整路径名,而不是让服务器决定默认文档。
  • 我运行了 Fiddler,但请求未显示在 Web 会话历史记录窗口中。
【解决方案2】:

或者,您可以尝试使用 HttpRequestObject 的 AllowAutoRedirect 属性。

【讨论】:

    【解决方案3】:

    我需要替换下面这行代码:

    request.Credentials = new NetworkCredentials("myaccount", "mypassword", "mydomain");
    

    与:

    request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
    

    【讨论】: