Doesn't generate complete response from HttpWebResponse object in C #
I am creating an HttpWebRequest object from another aspx page to store the response stream in my datastore. The Url I am using to create the HttpWebRequest object has a querystring function to display the correct output. When I view the page using an old browser, it displays correctly. When I try to get the output stream using HttpWebResponse.GetResponseStream (), it does my inline error checking.
Why does it display correctly in the browser, but not using the HttpWebRequest and HttpWebResponse objects?
Here's the source code:
The code behind the landing 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 the page that creates the HttpWebRequest object
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 an HttpWebRequest with the full page name. The page is still generating error output. I've updated the landing page code sample to show exactly what I'm doing.
@Chris Lively - I am not redirecting to an error page, I am generating a message indicating that the query string value was not found. I have updated the source code.
Update 1:
I tried using Fiddler to track the HttpWebRequest and it was not showing up in the web session history window. I am missing something in my original code to get a complete web request and response.
Update 2:
In my example, I did not include the following section of code and it was the culprit for this issue. I set the property Credentials
to HttpWebRequest
with the sevice account instead of my AD account which was causing the problem.
I have updated the source code
source to share
What web server are you using? I remember sometime in my past when I was doing something with IIS, there was a problem where a redirect between http://example.com/ and http://example.com/default.asp removed the query string.
Perhaps run Fiddler (or a protocol sniffer) and see if there is anything you are not expecting.
Also check if passing the full page name works. If he does this, it is almost certainly a problem.
source to share
Optionally, you can try using the AllowAutoRedirect property of the HttpRequest object.
source to share