WebRequest - What are these statements doing?

D

Dave56

I have an asp.net web service that is posting data to another web server.
My code (somewhat simplified) looks like this:

'Convert string to byte array

Dim bodyBytes() As Byte = encoding.GetBytes(myString)



'Configure Http Request

Dim myWebRequest As WebRequest = WebRequest.Create(myURL)

myWebRequest.Method = "POST"

myWebRequest.ContentLength = bodyBytes.Length

myWebRequest.Timeout = 90000 '90 Seconds



'Fill the Http Request stream with XML byte array

Dim myRequestBodyStream As Stream = myWebRequest.GetRequestStream

myRequestBodyStream.Write(bodyBytes, 0, bodyBytes.Length)

myRequestBodyStream.Close()



'Get Http Response

Dim myResponse As WebResponse = myWebRequest.GetResponse

Dim myResponseStream As New
StreamReader(myResponse.GetResponseStream, System.Text.Encoding.ASCII)



'Get the Response

Dim myResponseBody As String = myResponseStream.ReadToEnd

myResponseStream.Close()


This is pretty much a black box to me...
I find that most of the time is spent on the myWebRequest.GetRequestStream
statement.

MSDN says:
- The GetRequestStream method initiates a request to send data to the
Internet resource and returns a Stream instance for sending data to the
Internet resource.

I guess I don't understand exactly what they mean by "initiates a request".

Is myWebRequest.GetRequestStream communicating with the other web server and
that is why it is taking longer?
Which statements are actually communicating with the other web server?
And which ones aren't?

Any help is greatly appreciated.
Dave
 
R

Robbe Morris [C# MVP]

This sends your data to them by grabbing a hold of the request stream and
writing
data to it.

Dim myRequestBodyStream As Stream = myWebRequest.GetRequestStream
myRequestBodyStream.Write(bodyBytes, 0, bodyBytes.Length)

myRequestBodyStream.Close()


This gets the response stream and shoves it in a local stream reader. So,
yes this
is why it takes so long

Dim myResponseStream As New
StreamReader(myResponse.GetResponseStream, System.Text.Encoding.ASCII)

This takes the local streamreader and converts it to a string.

Dim myResponseBody As String = myResponseStream.ReadToEnd
myResponseStream.Close()



--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.masterado.net

Earn $$$ money answering .NET Framework
messageboard posts at EggHeadCafe.com.
http://www.eggheadcafe.com/forums/merit.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top