views:

1153

answers:

4

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.

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

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

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.

@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.

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.

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.

I updated my source code example

+2  A: 

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.

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

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

David McEwing
I am using IIS 5.1 to test on my local workstation, then it will deployed to an IIS 6.0 server.
Michael Kniskern
That was the config when I had the problem. Try passing the full path name rather than letting the server decide the default document.
David McEwing
I updated my answer based on your response
Michael Kniskern
I ran Fiddler and the request did not show up in Web Session history window.
Michael Kniskern
A: 

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

Chris Lively
I updated my answer based on your response
Michael Kniskern
A: 

I need to replace the following line of code:

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

with:

request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
Michael Kniskern
A: 

Hi, I used the same code but its not working.... I am not able to get complete response.... how can I get it?

Did you try using the solution in the selected answer?
Michael Kniskern