WebRequest & WebResponse

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&#151which one is better really just depends on your priorities).

Method 1:

Dim strXml As StringstrXml = "XML= Sample Info To Send" '"XML =" is the field name in thiscase (check documentation of web service)Dim sURL As String = "http://myAddress.com/myPage.asp" 'address of webserviceDim req As HttpWebRequest = HttpWebRequest.Create(sURL) 'you must use theCREATE (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 anythingreq.ContentLength = CLng(strXml.Length) 'this defaults to -1 (assumesyou'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 aStreamWriter,'so I create one from the stream)Dim s As Streams = req.GetRequestStream 'gives you a streamDim sw As StreamWriter = New StreamWriter(s)sw.Write(strXml)'you MUST close these NOW, or you can blow up the connection to the webservicesw.Close()s.Close()'Get the responseDim resp As WebResponse = req.GetResponse() 'I could also use aHttpWebResponse 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 upsResp.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 serviceaddress - replace with real oneDim strXML As String = "Sample" 'will be XML string to sendDim xmlhttp As New MSXML2.XMLHTTP() 'Start of code to use HTTP POSTxmlhttp.open("POST", sURL, False) 'POST is the method, sURL is the webservice, False = NOT asyncxmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded") 'headers (and their values) vary byweb service - this is just an examplexmlhttp.send("XML=" & strXML) ' "XML = " is the "field" name in thiscase - varies by web service - again, this is just an example of somethingto sendTextBox1.Text = xmlhttp.responseXML.xml 'puts any response string into atextbox for display
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes