![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
Unfortunately, certain characters have special meanings to the HTTP protocol. Space and carriage return characters can cause the data to be broken. That means a program cannot safely include text that contains these characters. If a program included the text here is some text with spaces, it would arrive at the server application truncated to here. By convention, a program should convert spaces into plus signs (+) before sending the data. The program at the other end can then convert the plus signs back into spaces. The InetXfer ActiveX control performs this and a few other translations to safely package the data before sending it to the server. InetXferThe InetXfer ActiveX control provides no properties and is not visible at run time. It provides only three methods: ClearData, AddData, and SendData. An application can use these methods to package data for transport across the Internet. ClearData ClearData empties the controls data buffer. The data is stored in the module-level string variable out_data. An application should invoke ClearData before it starts adding data to the transfer buffer.
Private out_data As String
Public Sub ClearData()
out_data =
End Sub
AddData The AddData method takes as parameters the name of a data item and its value. It uses the AppendSegment subroutine to add the name and value to the out_data buffer. AppendSegment protects special characters such as spaces and carriage returns in the string. It then appends the result to out_data. AppendSegment, its inverse function RemoveSegment, and their support routines are described later in the section Packing and Unpacking Data.
Public Sub AddData(ByVal name As String, ByVal value As String)
AppendSegment out_data, Trim$(name)
AppendSegment out_data, Trim$(value)
End Sub
SendData The SendData method actually sends the data to a remote program using an Internet transfer control. SendData takes as a parameter the URL of the program that should receive the data. It combines the URL with the information in out_data, separated by a question mark. It then uses the Internet transfer controls Execute method to send the data.
Public Sub SendData(url As String)
Inet1.Execute url & ? & out_data
End Sub
Because it takes the URL as a parameter, one might think the SendData method was not safe for scripting. After all, a script could use any value for the URL and make SendData transfer data to any location. This is nothing a script cannot do anyway. Using HTML code, a script can invoke a command that contains a URL followed by a question mark and a string of data in any case. SendData does not greatly increase the danger of the control. Sending Data in Survey.HTM When the user clicks WebSurveys Send Data button, the VBScript event handler SendButton_OnClick sends the data. First, it uses the InetXfer controls ClearData method to empty the data buffer. Next, it uses the AddData method to place data items in the data buffer. It then invokes the InetXfer controls SendData method to transfer the data to the server program http://Beauty/Scripts/SaveData.EXE. You will need to change the location of the server before you can run the code on your computer. Finally, the event handler clears the form and presents a message to the user saying the data has been sent.
Sub SendButton_OnClick
Empty the data buffer.
InetXfer1.ClearData
Build up the data to send.
InetXfer1.AddData QAlgs, RadioValue(QAlgs)
InetXfer1.AddData QGraphic, RadioValue(QGraphic)
InetXfer1.AddData QWeb, RadioValue(QWeb)
InetXfer1.AddData Access, _
Access.Options(Access.SelectedIndex).Value
InetXfer1.AddData MainLang, _
MainLang.Options(MainLang.SelectedIndex).Value
InetXfer1.AddData Name, NameText.Value
InetXfer1.AddData Email, EmailText.Value
InetXfer1.AddData Street, StreetText.Value
InetXfer1.AddData City, CityText.Value
InetXfer1.AddData State, StateText.Value
InetXfer1.AddData Zip, ZipText.Value
InetXfer1.AddData Phone, PhoneText.Value
InetXfer1.AddData SendCopy, SendCopy.Checked
InetXfer1.AddData Comments, CommentText.Value
Send the data.
InetXfer1.SendData <http://Beauty/Scripts/SaveData.exe>
Clear the form.
ClearForm
Tell the user the data has been sent.
MsgBox The data has been sent. Thank you for your time!
End Sub
Receiving Data across the WebThe SaveData program, included in the Ch18\SaveData subdirectory of the compact disk, receives the data sent by Survey.HTM. The program has no visible interface. It begins execution with the Main subroutine. The program uses Visual Basics Environ function to retrieve the value of the QUERY_STRING environment variable. This string contains the data that was packaged by the InetXfer control in Survey.HTM. The string contains field value names and data values separated by semi-colons. Special characters have been protected by converting them into some other character; for example, spaces have been replaced with plus signs. The following text shows a portion of what the data might look like at this point. The translation process is described further in the section Packing and Unpacking Data. Access;Only+Phone+Line;MainLang;VB;Name;Rod+Stephens;... Once it has read the QUERY_STRING environment variable, the program opens the data file where it will store the data values. SaveData then uses the RemoveSegment function to pull a piece of data out of the environment variable string. It truncates data field names to 8 characters and data values to 100 characters. This protects the program from accidental or intentional misuse. For instance, a user could enter the following URL into a browser to send the program a long string of garbage: <http://beauty/SaveData.exe?Name;An+extremely+long+piece+of+garbage...> Someone can still send the program bad data, but the damage will be minimal.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|