By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,649 Members | 1,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,649 IT Pros & Developers. It's quick & easy.

WebRequest - What are these statements doing?

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

"Dave56" <no****@co.lane.or.us> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...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

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.