Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

HTTP Posting with VB

Posted on 1999-07-20
8
Medium Priority
?
998 Views
Last Modified: 2008-11-09
With a VB program, i need to post something to the webserver, how can i do it? (just like someone was pressing on "Submit" from an html form...)
0
Comment
Question by:OeilNoir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 13

Accepted Solution

by:
Mirkwood earned 400 total points
ID: 1526167


HOWTO: Use the PostData Parameter in WebBrowser Control
Last reviewed: August 7, 1998
Article ID: Q174923
 


--------------------------------------------------------------------------------

The information in this article applies to:
Microsoft Visual Basic for Windows Professional and Enterprise Editions, version 6.0
Microsoft Visual Basic Control Creation and Enterprise Editions for Windows, version 5.0
Microsoft Visual Basic Enterprise Edition, 16-bit and 32-bit, for Windows, version 4.0
Microsoft ActiveX SDK, version 1.0 on the following platforms: NT, Win95, x86
Internet Explorer, versions 3.0, 3.01, 3.02, 4.0, 4.01
Internet Client SDK, version 4.0, 4.01


SUMMARY
The WebBrowser control has a Navigate method with PostData as a parameter. This allows the user to post data to the specified URL with the WebBrowser control.



MORE INFORMATION
Navigate specifications:


   Navigates to the resource identified by a Universal Resource Locator
   (URL), or to the file identified by a full path.

Syntax:

   object.Navigate URL [Flags,] [TargetFrameName,] [PostData,] [Headers]

PostData Optional:
Data to send to the server during the HTTP POST transaction. For example, the POST transaction is used to send data gathered by an HTML form. If this parameter does not specify any post data, the Navigate method issues an HTTP GET transaction. This parameter is ignored if URL is not an HTTP URL.

NOTE: The post data specified by PostData is passed as a SAFEARRAY structure. The variant should be of type VT_ARRAY and point to a SAFEARRAY. The SAFEARRAY should be of element type VT_UI1, dimension one, and have an element count equal to the number of bytes of post data.

Sample to PostData to URL server


Start Visual Basic. If Visual Basic is already running, choose New Project from the File menu (ALT, F, N). Form1 is created by default.

Add a WebBrowser control (WebBrowser1) to Form1.

Add a CommandButton (Command1) to Form1.

Add the following code to the Command1_Click event of Form1:

      Sub Command1_Click()
         Dim URL As String
         Dim Flags As Long
         Dim TargetFrame As String
         Dim PostData() As Byte
         Dim Headers As String

         URL = "http://YourServer" ' A URL that will accept a POST
         Flags = 0
         TargetFrame = ""

         PostData = "Information sent to host"

         ' VB creates a Unicode string by default so we need to
         ' convert it back to Single byte character set.
         PostData = StrConv(PostData, vbFromUnicode)

         Headers = "Content-Type: application/x-www-form-urlencoded" & _
            vbCrlf
         WebBrowser1.Navigate URL, Flags, TargetFrame, PostData, Headers
      End Sub


Press the F5 key to run the program and the information in the PostData variable will be sent to the server specified in the URL parameter.
 



--------------------------------------------------------------------------------
Additional query words: POST Navigate kbdsi kbDSupport kbVBp kbVBp500
kbVBp600 kbIndetDev400 kbIE300 kbIE301 kbIE302 kbIE400 kbIE401
Platform : NT Win95 WINDOWS
Hardware : x86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.  

Last reviewed: August 7, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.


0
 
LVL 1

Author Comment

by:OeilNoir
ID: 1526168
Thanks for the help, but there is still something that isn't clear...
usualy, when i do a post on a server,i'm sending variables, with their values...

here, all what is see is that :
->      PostData = "Information sent to host"

isn't there a certain way this have to be formatted? how do i tell it what are the variables and the values?
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1526169
You are right. This example sucked

HOWTO: Automate Internet Explorer to POST Form Data
Last reviewed: January 19, 1998
Article ID: Q167658
 


--------------------------------------------------------------------------------

The information in this article applies to:
Microsoft ActiveX SDK, version 1.0
Internet Client SDK, versions 4.0, 4.01
Microsoft Internet Explorer (Programming), versions 3.0, 3.01, 3.02, 4.0


SUMMARY
An automation controller such as Microsoft Visual Basic can automate Internet Explorer (IE) to send data using the POST method to an HTTP server such as Internet Information Server (IIS). This article demonstrates how to automate IE to perform a POST of form data from Visual Basic and from a simple Visual C++ application. While the examples illustrate automation of the whole Internet Explorer application, the same techniques can be applied to the Web Browser ActiveX control when it is hosted in an application.



MORE INFORMATION
Several methods are available for sending data to an HTTP server. GET and POST are currently the most common. POST is typically used to submit form data to an HTTP server when that data exceeds the maximum allowable transfer using the GET method. This limit is typically 2K.

IE exposes the IWebBrowser(App) interface to automation controllers. The IWebBrowser(App) interface exposes a Navigate method. In interface definition language (IDL) syntax (a universal representation from which both the Visual Basic and Visual C++ syntax is derived), the Navigate method looks like the following:


   HRESULT Navigate([in] BSTR URL, [in] VARIANT* Flags,
                [in] VARIANT* TargetFrameName, [in] VARIANT* PostData,
                [in] VARIANT* Headers);

For the purposes of performing a POST, only the URL, PostData, and Headers parameters are relevant. See the Internet Client SDK documentation for information on the other parameters.
To call Navigate and POST form data to an HTTP server, the URL parameter must specify a valid address, the PostData parameter must contain a SAFEARRAY of bytes, and the Headers parameter must contain a BSTR that contains the following HTTP header:


   Content-Type: application/x-www-form-urlencoded

This header indicates that the data being posted is encoded according to the rules specified in the HTML specification.
Internet Explorer versions 3.0, 3.01, and 3.02 do not support posting data to an HTTP server via the Navigate method. WinInet and URL Monikers are two possible alternatives. For more information on WinInet and Monikers, see the REFERENCES section of this article.

Note that the Internet Explorer Script Object Model object "window" has a "navigate" method as well. This navigate method will only accept a URL and cannot be used to POST data to a web server.

To test the examples below, the following Active Server Page (ASP) should be saved to the file NAVPOST.ASP in a directory on an NT 4.0 server running Internet Information Server (IIS) version 3.0. The directory should be recognized by IIS as a virtual root with execute permission:


   <HTML>
   <%
   cFlavor = Request("Flavor")
   cName = Request("FName")
   %>
   <BODY>
   Hello, <% =cName %>. <br>
   One scoop of <% =cFlavor %> coming right up!
   </BODY>
   </HTML>

Visual Basic
To demonstrate a POST in Visual Basic, perform the following steps:


Start a new Standard EXE project in Visual Basic. Form1 is created by default. Add the following controls to Form1:

   Object                     Name                  Caption
   -------------------------------------------------------------------

   Label                      lblName                First Name
   Label                      lblFlavor              Flavor
   CommandButton              cmdSubmit              Submit

   ComboBox                   cboFlavor
   TextBox                    txtName
   TextBox                    edtPostData

   (edtPostData should be multi-line and have scroll bars)


Invoke "References" from the Project menu and add a reference to the project for "Microsoft Internet Controls" (SHDOCVW.DLL). Make sure this is done from the References dialog and not just from the Components dialog.

Insert the following code in the General Declarations section:


      Dim g_oIE As InternetExplorer ' Global reference to an
                                    ' instance of IE
      Private Sub cmdSubmit_Click()
      edtPostData.Text = ""  ' Initialize an edit box for testing
      ReDim aByte(0) As Byte ' Array of bytes to hold data to post

      ' Extract the URL encoded data from the UI,
      ' and pack it into an array of bytes
      cFlavor = cboFlavor.List(cboFlavor.ListIndex)
      cParamName = "FName="
      cParamFlavor = "Flavor="
      cSeparator = "&"
      cPostData = cParamName & txtName.Text _
         & cSeparator & cParamFlavor & cFlavor
      PackBytes aByte(), cPostData

      ' For testing, rebuild the POST data and stuff
      ' it into an edit box
      For i = LBound(aByte) To UBound(aByte)
          edtPostData = edtPostData + Chr(aByte(i))
      Next

      Dim vPost As Variant
      vPost = aByte ' Assign the byte array to a VARIANT
      Dim vFlags As Variant
      Dim vTarget As Variant
      Dim vHeaders As Variant
      vHeaders = _
         "Content-Type: application/x-www-form-urlencoded" _
         + Chr(10) + Chr(13)

      ' You're done. Now call Navigate
      ' Note: modify path to server as appropriate
      g_oIE.Navigate "http://<server>/scripts/navpost.asp", _
         vFlags, vTarget, vPost, vHeaders

      End Sub

      Private Sub Form_Load()
      ' Create an instance of IE
      Set g_oIE = New InternetExplorer
      g_oIE.Visible = True

      ' Populate a combobox with some flavor choices
      cboFlavor.List(0) = "Vanilla"
      cboFlavor.List(1) = "Chocolate"
      cboFlavor.List(2) = "Strawberry"
      cboFlavor.ListIndex = 0 ' The default choice
      End Sub

      ' Utility function to pack the data to post into an array of bytes
      Private Sub PackBytes(ByteArray() As Byte, ByVal PostData As String)
      iNewBytes = Len(PostData)
      If iNewBytes = 0 Then
         Exit Sub
      End If
      iCursize = UBound(ByteArray) - LBound(ByteArray)
      ReDim Preserve ByteArray(iNewBytes + iCursize)
      For i = 1 To iNewBytes
         ch = Mid(PostData, i, 1)
         If ch = Space(1) Then
            ch = "+"
         End If
         ByteArray(i + iCursize - 1) = Asc(ch)
      Next
      End Sub


Modify the URL in the call to Navigate as appropriate, close all running instance of IE, and choose Start from the Run menu.

Enter your name in the first name textbox, choose a flavor, and click the Submit command button. The data from the Visual Basic form will be posted to the HTTP server and the response will appear in the visible browser window.  

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:OeilNoir
ID: 1526170
mmm... the exemple isn't working..

Run Time Error 430 :
Class does not support Automation or does not support exepected interface

Anyway, i think i still managed to figure out what i wanted to know... that is, how they were formating the Post variable.

So, if i understood well.. what they put in post, is simply the what we could add to the url to pass some parameters.? Exemple :

with this url:
http://www.url.com/path/file.ext 

if i have the parameters :
Variable1 and Variable2

i could either use

vPost = variable1=value1&variable2=value2
Headers = "Content-Type: application/x-www-form-urlencoded" & vbCrLf
'no matter what th vFlags are VTarget are...
object.Navigate "http://www.url.com/path/file.ext", vFlags, vTarget, vPost, vHeaders

Or i could do :

Headers = "Content-Type: application/x-www-form-urlencoded" & vbCrLf
object.Navigate "http://www.url.com/path/file.ext?variable1=value1&variable2=value2", , , , Headers


So, basicaly, my question is : is it the same thing to put the "variables strings" in the VPost Variable or to just concatenate it to the url?
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1526171
With the right quotation"", both will work..
0
 
LVL 1

Author Comment

by:OeilNoir
ID: 1526172
Thanks, i guess it answer my question....

just one more thing, what do you mean exactly by "with the right quotation"?

0
 
LVL 1

Author Comment

by:OeilNoir
ID: 1526173
Also, i was wondering, could i get the same result with inet1.openurl ?
(that is to post)
0
 
LVL 1

Author Comment

by:OeilNoir
ID: 1526174
The reason i ask that, is that when i do the post, i must find a way to verify the result, did it worked? and etc.... or, is there a way to get "see" the html code with the webbrowser control?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question