Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: .NET
Expertise: Intermediate
Mar 9, 2004

WebRequest & WebResponse

There are two methods to send info and get a response via HTTP. The second (alternate) method works for both .NET and VB6, but it requires DLL reference (take your pick between these methods—which one is better really just depends on your priorities).

Method 1:


Dim strXml As String
strXml = "XML= Sample Info To Send" '"XML =" is the field name in this
case (check documentation of web service)

Dim sURL As String = "http://myAddress.com/myPage.asp" 'address of web
service
Dim req As HttpWebRequest = HttpWebRequest.Create(sURL) 'you must use the
CREATE (NOT New)
'Alternate (from MSDN - I didn't test this) ->
' Dim req As HttpWebRequest = CType(WebRequest.Create(sURL),
HttpWebRequest)
req.ContentType = "application/x-www-form-urlencoded" 'header info (again,
check documentation of web service)
'you must set the ContentLength to send anything
req.ContentLength = CLng(strXml.Length) 'this defaults to -1 (assumes
you're going to do a GET)
'Method property can be set to any of the HTTP 1.1 protocol verbs: GET,
HEAD, POST, PUT, DELETE, TRACE, or OPTIONS
'Do NOT change the Method property AFTER you've started the request
(req.Get...) - will throw error (Invalid Operation Exception)
req.Method = "POST"
'HttpWebRequest.GetRequestStream is how you "send" stuff -
'it gives you a stream you can write to (in this case, easier to use a
StreamWriter,
'so I create one from the stream)
Dim s As Stream
s = req.GetRequestStream 'gives you a stream
Dim sw As StreamWriter = New StreamWriter(s)
sw.Write(strXml)

'you MUST close these NOW, or you can blow up the connection to the web
service
sw.Close()
s.Close()

'Get the response
Dim resp As WebResponse = req.GetResponse() 'I could also use a
HttpWebResponse here (use CType) - IN THEORY (didn't test)
Dim sResp As StreamReader = New StreamReader(resp.GetResponseStream)
TextBox1.Text = sResp.ReadToEnd 'display response in textbox

'clean up
sResp.Close()
resp.Close()
req = Nothing
The next method is the VB6 equivalent done .NET-style. This method does work, but you need to remember to add a reference to the MSXML DLL (usually in system32 folder—use version 3 at least).

Method 2:


Dim sURL As String = "http://myAddress.com/myPage.asp" 'bogus web service
address - replace with real one
Dim strXML As String = "Sample" 'will be XML string to send
Dim xmlhttp As New MSXML2.XMLHTTP() 'Start of code to use HTTP POST

xmlhttp.open("POST", sURL, False) 'POST is the method, sURL is the web
service, False = NOT async
xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded") 'headers (and their values) vary by
web service - this is just an example
xmlhttp.send("XML=" & strXML) ' "XML = " is the "field" name in this
case - varies by web service - again, this is just an example of something
to send
TextBox1.Text = xmlhttp.responseXML.xml 'puts any response string into a
textbox for display
Julie Ferm
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date