Http Post Example

When communicating with a web service via HTTP POST, data is embedded as name-value pairs in the HTTP request's message body. Unlike HTTP GET, the name-value pairs do not appear as part of the URI.

In this example we call the Calculator component that we developed in the Web Service Example section of this chapter. Unlike in the Web Service Example, however, we will not use the web browser to communicate via HTTP GET. Instead, we will use a properly formatted HTTP POST request to access the Calculator service. To this end, we employ an HTTP-capable component (Microsoft's MSXML COM Component, version 3.0) from Visual Basic 6.0. The use of VB6 is intentional—it demonstrates that web services can be used outside of .NET with any client capable of sending and receiving HTTP requests. Realize also that our client need not be a Windows component. The client could just as easily reside on a Linux or Macintosh machine.

In Figure 9.1 we showed the default page generated by ASP.NET for the Calculator service's GetRandomNumber() method. Revisit this page by launching IE with the following URL:


Scroll down this page and look under the heading HTTP POST. This section of the page presents the message format required to access the Calculator service via HTTP POST. The request and response messages appear as follows:

POST /myservice/Calculator.asmx/GetRandomNumber HTTP/1.1 Host: localhost

Content-Type: application/x-www-form-urlencoded Content-Length: length x=string&y=string

Listing 9.2 Sample HTTP POST request for the Calculator service

HTTP/1.1 200 OK

Content-Type: text/xml; charset=utf-8 Content-Length: length

<?xml version="1.0" encoding="utf-8"?> <double xmlns=""> double</double>

Listing 9.3 Sample HTTP POST response from the Calculator service

The first four lines of Listing 9.2 are the required headers for the HTTP POST request. Note that /myservice/Calculator.asmx/ GetRandomNumber is the URI of the resource we are requesting—the Calculator service's GetRandomNumber() method. The Content-Type specifies the format of the data in the message body. "x-www-form-url-encoded" specifies that data is url-encoded as name-value pairs in the message.

Listing 9.3 describes the format of results returned from the web service. Of particular importance is the line "Content-Type: text/xml". Recall that results from a .NET web service are encoded as XML, regardless of the protocol used to request the service (HTTP GET/POST or SOAP). Note also that the XML result from the service (the <double>

element) is prefixed with the XML namespace we used in Listing 9.1 ( Remember, this does not refer to a resource on the web—it is a namespace that distinguishes the Calculator's double result from other web services on the Internet.

Using Listing 9.2 as a template, we now write a Visual Basic application that calls the GetRandomNumber() method. You can obtain the full source code online at ^^NET090003, along with compiled binaries for those who do not have access to a Visual Basic compiler. Alternatively, create a new Standard EXE project in VB6. In the Projects menu, select References and add a reference to the Microsoft XML, v3.0 component (WinNT\System32\msxml3.DLL). Add a textbox and one button to your form so that it resembles the interface in Figure 9.3.

Vb6 Http Post
Figure 9.3 Visual Basic HTTP POST application

Double-click on the HTTP POST button and add the code in Listing 9.4 to the button's click event. This code uses the MSXML component to call GetRandomNumber(), placing the obtained results in the textbox.

'Instantiate the MSXML component Dim req As New MSXML2.XMLHTTP30

'Specify the URI of our component

'(the second variable specifies synchronous behavior) "POST",

"http://localhost/myservice/Calculator.asmx/GetRandomNumber", False 'Set HTTP Headers req.setRequestHeader "Host", "localhost"

req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

'Send the HTTP POST request and place results in the textbox req.send "x=1&y=100"

Dim responseStr As String responseStr = "Content-Type: " &

req.getResponseHeader("Content-Type") & vbCrLf responseStr = responseStr & "Content-Length: " &

req.getResponseHeader("Content- Length") & vbCrLf responseStr = responseStr & req.responseText Textl.Text = responseStr

Listing 9.4 Calling GetRandomNumber() using HTTP POST

The line specifies the HTTP Protocol, POST, as well as the URI of the GetRandomNumber() method. The False parameter tells the MSXML component to send the HTTP request synchronously—that is, after calling req.send(), block program execution until a response is received.

The two req.setRequestHeader() calls set the required HTTP headers Host and Content-Type. You might notice that the Content-Length header from Listing 9.2 is omitted. This header (the length in bytes of the HTTP message) is automatically calculated and inserted by the MSXML component.

The req.send() call appends the url-encoded string to the body of the HTTP message and sends the message to the specified HOST. When the HOST responds, the message is displayed in the form's textbox (Figure 9.3). Run the application by pressing F5, click the HTTP POST Request button, and you will see the following response in the textbox:

Content-Type: text/xml; charset=utf-8 Content-Length: 120

<?xml version="1.0" encoding="utf-8"?> <double xmlns=""> 57.845199582560547</double>

Listing 9.5 HTTP response message

Listing 9.5 shows that the web service responds to our request with the format described in Listing 9.3.

+2 -1

Post a comment