473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

WebRequest - What are these statements doing?

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
1 1320
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: John K. | last post by:
Hi I was wondering if it's possible to use the WebRequest class to access a file on windows shared folder with authentication? If yes, what would the syntax be? I've tried to look this up in the...
4
by: Terry | last post by:
Hello, I am trying to get a response for an .aspx page in my current project (same virtual directory) by using WebRequest.GetResponse but I keep getting a exception with "500 Internal server...
5
by: james.dixon | last post by:
Hi I have been struggling with what should be a simple thing. I want to crawl the web for specific links, and save any html and files that meet my criteria to my hard drive. I thought that...
0
by: buccsailor | last post by:
Hell there, I tried to post a reply to a message thread created back in July 2006 regarding the override of WebRequest, Closed Conenctions and setting KeepAlive to false,but it's been over 60...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.