没有从C#中的HttpWebResponse对象生成完整的响应

时间:2022-02-23 15:12:05

I am creating a HttpWebRequest object from another aspx page to save the response stream to my data store. The Url I am using to create the HttpWebRequest object has querystring to render the correct output. When I browse to the page using any old browser it renders correctly. When I try to retrieve the output stream using the HttpWebResponse.GetResponseStream() it renders my built in error check.

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

Why would it render correctly in the browser, but not using the HttpWebRequest and HttpWebResponse objects?

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

Here is the source code:

这是源代码:

Code behind of target page:

目标页面背后的代码:

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);
}

Code behind of page creating HttpWebRequest object

创建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();

Update

@David McEwing - I am creating the HttpWebRequest with the full page name. The page is still generating the error output. I updated the code sample of the target page to demonstrate exactly what I am doing.

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

@Chris Lively - I am not redirecting to an error page, I generate a message indicating the query string value was not found. I updated the source code example.

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

Update 1:

I tried using Fiddler to trace the HttpWebRequest and it did not show up in the Web Sessions history window. Am I missing something in my source code to get a complete web request and response.

我尝试使用Fiddler来跟踪HttpWebRequest,它没有显示在Web Sessions历史记录窗口中。我在源代码中遗漏了一些内容,以获得完整的Web请求和响应。

Update 2:

I did not include the following section of code in my example and it was culprit causing the issue. I was setting the Credentials property of the HttpWebRequest with a sevice account instead of my AD account which was causing the issue.

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

I updated my source code example

我更新了我的源代码示例

3 个解决方案

#1


What webserver are you using? I can remember at one point in my past when doing something with IIS there was an issue where the redirect between http://example.com/ and http://example.com/default.asp dropped the query string.

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

Perhaps run Fiddler (or a protocol sniffer) and see if there is something happening that you aren't expecting.

也许运行Fiddler(或协议嗅探器),看看是否有一些你不期待的事情发生。

Also check if passing in the full page name works. If it does the above is almost certainly the problem.

还要检查传入完整页面名称是否有效。如果它确实如上所述几乎肯定是问题。

#2


Optionally, you can try to use the AllowAutoRedirect property of the HttpRequestObject.

(可选)您可以尝试使用HttpRequestObject的AllowAutoRedirect属性。

#3


I need to replace the following line of code:

我需要替换以下代码行:

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

with:

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

#1


What webserver are you using? I can remember at one point in my past when doing something with IIS there was an issue where the redirect between http://example.com/ and http://example.com/default.asp dropped the query string.

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

Perhaps run Fiddler (or a protocol sniffer) and see if there is something happening that you aren't expecting.

也许运行Fiddler(或协议嗅探器),看看是否有一些你不期待的事情发生。

Also check if passing in the full page name works. If it does the above is almost certainly the problem.

还要检查传入完整页面名称是否有效。如果它确实如上所述几乎肯定是问题。

#2


Optionally, you can try to use the AllowAutoRedirect property of the HttpRequestObject.

(可选)您可以尝试使用HttpRequestObject的AllowAutoRedirect属性。

#3


I need to replace the following line of code:

我需要替换以下代码行:

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

with:

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