HttpWebRequest.GetResponse not Synchronous ?

  • Thread starter Thread starter Ben Sudbury
  • Start date Start date
B

Ben Sudbury

Hi There,

I have some code that is designed to display an image from another webserver
as if it was coming from the server where the code is running. Please see
below:

private void OutputContent(String strURL)

{


page_Output.Trace.Write ("Start Output Content");

HttpWebRequest thisRequest = (HttpWebRequest)WebRequest.Create(new
System.Uri(strURL));


HttpWebResponse thisResponse = (HttpWebResponse)thisRequest.GetResponse();

//**************
System.Threading.Thread.Sleep(500);


page_Output.Trace.Write ("Content Length is " +
thisResponse.ContentLength.ToString() );

page_Output.Response.Clear();

page_Output.Response.ContentType = thisResponse.ContentType;

BinaryReader readerResponse = new
BinaryReader(thisResponse.GetResponseStream());


int readBytes = 0;

int bufsize = 4096;

byte [] responseBuffer = new byte[bufsize];


//FileStream fs = new FileStream(@"c:\aspnetarea\test.gif",FileMode.Create,
FileAccess.Write);


string strResult = null;

//page_Output.Trace.Write ("Reader Length is " +
readerResponse.BaseStream.ToString() );




while((readBytes = readerResponse.Read(responseBuffer, 0, bufsize)) > 0)

{

//System.Threading.Thread.Sleep(1000);

//strResult = System.Text.Encoding.ASCII.GetString(responseBuffer);

//page_Output.Trace.Write ("ReadBytes Says " + readBytes.ToString() );

//page_Output.Trace.Write ("Buffer Contains (" + strResult.Length + ") " +
strResult );

page_Output.Response.BinaryWrite(responseBuffer);

//fs.Write(responseBuffer, 0, responseBuffer.Length);

}

readerResponse.Close();

//fs.Close();

}

page_Output is the System.Web.UI.Page instance that we are writing the image
out to

As an example if I call

OutputContent(http://www.fantasticfurniture.com.au/3dlogo.gif);

and leave out the line

//**************
System.Threading.Thread.Sleep(500);

I only get half an image.

If I insert the line to sleep for 500 milliseconds after Getting the
response, it usually works but displays only half an image sometimes

If I set the sleep time higher still it is 100% reliable.

If I try the same code in a console application with the static main method
calling the my function (modified to write the stream to a file instead of
the screen) I get a strange looking image without putting the thread to
sleep and the correct image if the thread is put to sleep.

My only conclusion is that somehow this method is not really Synchronous.
Either that or the GetResponseStream method is not truly synchronous, though
it does seem to be the former from playing around with the location of the
sleep.

I am running the 1.1 version of the framework. Does anyone get the same
results. This experiment can be best demonstrated with slower internet sites
I would expect.

Is there anything I can check to ensure that the content is fully downloaded
before starting to read the stream. I can implement a sleep mechanism while
waiting for the OK from the framework, but I cannot just wait for a random
amount of time for the stream as I expect the time will vary wildly for
larger content. I really don't want to have to implement the full
asynchronous begingetrequest method if it can be avoided. Is there a better
/ reliable way to do this?

Regards,



Ben Sudbury
 
HttpWebResponse class has an property "StatusCode". You may check ihis
property before you get actual data from it. For example, call the
thread.sleep() in a loop until he StattusCode is "OK"

Luke

"Microsoft Security Announcement: Have you installed the patch for
Microsoft Security Bulletin MS03-026?? If not Microsoft strongly advises
you to review the information at the following link regarding Microsoft
Security Bulletin MS03-026
http://www.microsoft.com/security/security_bulletins/ms03-026.asp and/or to
visit Windows Update at http://windowsupdate.microsoft.com to install the
patch. Running the SCAN program from the Windows Update site will help to
insure you are current with all security patches, not just MS03-026."
 
Back
Top