Sending Data from the Client to the Server Application

Two mechanisms can be used to send information to a web server. You can either include the information as part of the URL string or send it as a separate section of the HTTP request.

URL Encoding

Parameters can be included within the URL string by appending them to the end of the URL, with a question mark (?) between the URL and the first parameter and an ampersand (&) between each parameter:

http://www.MySite.com/MyPage.asp?param1=value1&param2=value2&param2=value3

This has the advantage that the parameters form part of the URL and hence can be stored in the user's Favorites list or typed directly into the browser. It has the disadvantage that there is a limit to the total length of a URL (2,083 characters in Internet Explorer), restricting the amount of information than can be passed in this way.

POSTing Data

Whenever a request for a web page is sent to a web server, the request contains a large amount of information in various header records. This includes things like the client application type and version, the communication protocol and version, and user IDs and passwords. It also contains a POST field that can be used to send information to the server application.

Because there is virtually no limit to the amount of data that can be put in a POST field, it is the preferred way of transferring information to the server, and is the method used in most applications.

Most web page forms use the POST mechanism to send the completed form to the server for processing. Excel can mimic the web-based form by sending the same data to the same server in the same POST format, with the result being sent back as the new web page. The names of the parameters can be found by examining the source of the real web page form. The form controls will be surrounded by a pair of <form> and </form> tags, and each control within the form has a name attribute; those names are used as the parameter names when the form's data is sent to the server:

Sub SendDataUsingPOST()

Dim oInet As Inet

Dim lContent As Long

Dim sData As String

Dim sHeader As String

Dim sResult As String

'Create a new instance

of the Internet Transfer Control

Set oInet = New Inet

'Build the POST string

with the data to submit

sData = "Param1=Value1"

& "&" & _

"Param2=Value2"

& "&" & _

"Param3=Value3"

'Spaces must be replaced with + signs

sData = Replace(sData,

'Tell the POST that we'

re sending an encoded parameter list

sHeader = "Content-Type: application/x-www-form-urlencoded"

'Send the error information to the server

oInet.AccessType = icDirect oInet.Execute "http://www.mysite.com/mypage.asp", "POST", _ sData, sHeader

'Wait for the server to complete its work Do While oInet.StillExecuting DoEvents

Loop

'Retrieve the returned text lContent = oInet.GetHeader("content-length") sResult = oInet.GetChunk(lContent + 100)

End Sub

In this example, you're simply sending a simple list of name/value pairs to the server. If you need to send more complex data, you can send it as XML by providing the XML in the sData variable, with a header set to "Content-Type: text/xml".

0 0

Post a comment