Sending HTML forms with WINAPI 
Author Message
 Sending HTML forms with WINAPI

Hi.
I want to write a limited internet browser using the internet functions
provided by winapi.
I have a problem sending a html form. There are two methods how to send a
form in html: "get" which only calls a simple URL and "post". Post does
obviously not send the data within the URL, it somehow sends it later.
Does anybody know how to transmit data in a form with winapi using the
post-method?
Thanks a lot for your help in advance,
Sebastian


Thu, 27 Feb 2003 22:22:07 GMT  
 Sending HTML forms with WINAPI
Hi Sebastian,

Maybe the following function helps for getting started. I wrote it for use in Office VBA and it downloads the data directly into the
clipboard for pasting them into Excel/World but you could adjusted it for your purposes:

(by the way the "post"ed data are also transmitted at the same time but in the HTTP header)

-----------------------------------------------------------------------------
Option Explicit

Private Declare Function OpenClipboard _
                Lib "user32" _
              ( _
                ByVal hWnd As Long _
              ) _
                As Long

Private Declare Function CloseClipboard _
                Lib "user32" () _
                As Long

Private Declare Function EmptyClipboard _
                Lib "user32" () _
                As Long

Private Declare Function SetClipboardData _
                Lib "user32" _
              ( _
                ByVal wFormat As Long, _
                ByVal hMem As Long _
              ) _
                As Long

Private Declare Function InternetOpen _
                Lib "wininet.dll" _
                Alias "InternetOpenA" _
              ( _
                ByVal lpszCallerName As String, _
                ByVal dwAccessType As Long, _
                ByVal lpszProxyName As String, _
                ByVal lpszProxyBypass As String, _
                ByVal dwFlags As Long _
              ) _
                As Long

Private Declare Function InternetConnect _
                Lib "wininet.dll" _
                Alias "InternetConnectA" _
              ( _
                ByVal hInternetSession As Long, _
                ByVal lpszServerName As String, _
                ByVal nProxyPort As Integer, _
                ByVal lpszUsername As String, _
                ByVal lpszPassword As String, _
                ByVal dwService As Long, _
                ByVal dwFlags As Long, _
                ByVal dwContext As Long _
               ) _
                As Long

Private Declare Function InternetReadFile _
                Lib "wininet.dll" _
              ( _
                ByVal hFile As Long, _
                ByVal sBuffer As String, _
                ByVal lNumBytesToRead As Long, _
                lNumberOfBytesRead As Long _
              ) _
                As Integer

Private Declare Function HttpOpenRequest _
                Lib "wininet.dll" _
                Alias "HttpOpenRequestA" _
              ( _
                ByVal hInternetSession As Long, _
                ByVal lpszVerb As String, _
                ByVal lpszObjectName As String, _
                ByVal lpszVersion As String, _
                ByVal lpszReferer As String, _
                ByVal lpszAcceptTypes As Long, _
                ByVal dwFlags As Long, _
                ByVal dwContext As Long _
              ) _
                As Long

Private Declare Function HttpSendRequest Lib "wininet.dll" _
                Alias "HttpSendRequestA" _
              ( _
                ByVal hHttpRequest As Long, _
                ByVal HttpPost_Headers As String, _
                ByVal lHeadersLength As Long, _
                ByVal sOptional As String, _
                ByVal lOptionalLength As Long _
              ) _
                As Boolean

Private Declare Function InternetCloseHandle _
                Lib "wininet.dll" _
              ( _
                ByVal hInternetHandle As Long _
              ) _
                As Boolean

Private Declare Function HttpAddRequestHeaders _
                Lib "wininet.dll" _
                Alias "HttpAddRequestHeadersA" _
              ( _
                ByVal hHttpRequest As Long, _
                ByVal HttpPost_Headers As String, _
                ByVal lHeadersLength As Long, _
                ByVal lModifiers As Long _
              ) _
                As Integer

Private Declare Function GlobalAlloc _
                Lib "kernel32" _
              ( _
                ByVal wFlags As Long, _
                ByVal dwBytes As Long _
              ) _
                As Long

Private Declare Function GlobalLock _
                Lib "kernel32" _
              ( _
                ByVal hMem As Long _
              ) _
                As Long

Private Declare Sub CopyMemory _
                Lib "kernel32" _
                Alias "RtlMoveMemory" _
              ( _
                Destination As Any, _
                Source As Any, _
                ByVal Length As Long _
              )

Private Declare Function GlobalUnlock _
                Lib "kernel32" _
              ( _
                ByVal hMem As Long _
              ) _
                As Long

Public Function fncPostandDownloadtoClipboard _
              ( _
                ServerName As String, _
                EngineFullName As String, _
                PostParameters As String _
              ) _
                As Integer
 'function that posts a request to a server engine and
 'downloads the responce directly into the clipboard;
 'application enabled to 'read' CF_HTML format can convert
 'the clipboard contents subsequently
 '
 'declaration of variables
  Dim InternetOpen_hWnd         As Long
  Dim InternetConnect_hWnd      As Long
  Dim HttpOpenRequest_hWnd      As Long
  Dim HttpSendRequest_Result    As Boolean
  Dim ContinueDataCollection    As Boolean
  Dim DataChunkRetrieved        As Boolean
  Dim NumberOfBytestoRead       As String * 2048
  Dim NumberOfBytesRead         As Long
  Dim ServerResponce            As String
  Dim MemoryType                As Long
  Dim MemoryHeap                As Long
  Dim VirtualTextLocation       As Long
 '
 'custom error codes
  Const Success = 1
  Const Error_UnexpectedError = 0
  Const Error_InternetSessionFailed = (-1)
  Const Error_ServerConnectionFailed = (-2)
  Const Error_HttpRequestOpenFailed = (-3)
  Const Error_HttpRequestSendFailed = (-4)
  Const Error_OpenClipboardFailed = (-5)
 '
 'Win32 API constants
  Const HttpPost_Header = "Content-Type: application/x-www-form-urlencoded"
  Const INTERNET_OPEN_TYPE_PRECONFIG = 0
  Const INTERNET_SERVICE_HTTP = 3
  Const INTERNET_DEFAULT_HTTP_PORT = 80
  Const INTERNET_FLAG_RELOAD = &H80000000
  Const HTTP_ADDREQ_FLAG_ADD = &H20000000
  Const HTTP_ADDREQ_FLAG_REPLACE = &H80000000
  Const CF_TEXT = 1
  Const GMEM_SHARE = &H2000&
  Const GMEM_MOVEABLE = &H2
  Const GMEM_ZEROINIT = &H40
 '
 'initialize the result of the function to Error_UnexpectedError;
 'assume unexpected error
  fncPostandDownloadtoClipboard = Error_UnexpectedError
 '
 'initialize other variables wininet.dll
  InternetOpen_hWnd = 0
 '
 'initiate an intenet session with wininet.dll
  InternetOpen_hWnd = InternetOpen _
                    ( _
                      lpszCallerName:="http generic", _
                      dwAccessType:=INTERNET_OPEN_TYPE_PRECONFIG, _
                      lpszProxyName:=vbNullString, _
                      lpszProxyBypass:=vbNullString, _
                      dwFlags:=0 _
                    )
 '
 'if a new internet session with wininet.dll could not be established
 'exit the function and return Error_InternetSessionFailed
  If InternetOpen_hWnd = 0 Then
     fncPostandDownloadtoClipboard = Error_InternetSessionFailed
     Exit Function
  End If
 '
 'establish a connection to the server
  InternetConnect_hWnd = InternetConnect _
                   ( _
                     hInternetSession:=InternetOpen_hWnd, _
                     lpszServerName:=ServerName, _
                     nProxyPort:=INTERNET_DEFAULT_HTTP_PORT, _
                     lpszUsername:=vbNullString, _
                     lpszPassword:="HTTP/1.0", _
                     dwService:=INTERNET_SERVICE_HTTP, _
                     dwFlags:=0, _
                     dwContext:=0 _
                   )
 '
 'if a connection with the remote server could not be established
 'terminate the internet session with wininet.dll,
 'return Error_ServerConnectionFailed and exit the function
  If InternetConnect_hWnd = 0 Then
     Call InternetCloseHandle(hInternetHandle:=InternetOpen_hWnd)
     fncPostandDownloadtoClipboard = Error_ServerConnectionFailed
     Exit Function
  End If
 '
 'open a 'HttpRequest' object of "post" type with target engine (ASP etc.)
 'in the remote server
  HttpOpenRequest_hWnd = HttpOpenRequest _
                   ( _
                     hInternetSession:=InternetConnect_hWnd, _
                     lpszVerb:="POST", _
                     lpszObjectName:=EngineFullName, _
                     lpszVersion:="HTTP/1.0", _
                     lpszReferer:=vbNullString, _
                     lpszAcceptTypes:=0, _
                     dwFlags:=INTERNET_FLAG_RELOAD, _
                     dwContext:=0 _
                   )
 '
 'if a 'HttpRequest' object could not be created then
 'terminate the internet session with wininet.dll,
 'return Error_HttpRequestOpenFailed and exit the function
  If HttpOpenRequest_hWnd = 0 Then
     Call InternetCloseHandle(hInternetHandle:=InternetOpen_hWnd)
     fncPostandDownloadtoClipboard = Error_HttpRequestOpenFailed
     Exit Function
  End If
 '
 'an Internet client application that posts HTML form data to a remote server
 'must add a "Content-Type: application/x-www-form-urlencoded" HTTP header in
 'the POST request; so we added in the 'HttpRequest' object we have opened
  Call HttpAddRequestHeaders _
                   ( _
                     hHttpRequest:=HttpOpenRequest_hWnd, _
                     HttpPost_Headers:=HttpPost_Header & vbCrLf, _
                     lHeadersLength:=Len(HttpPost_Header & vbCrLf), _
                     lModifiers:=HTTP_ADDREQ_FLAG_REPLACE Or HTTP_ADDREQ_FLAG_ADD _
                   )
 '
 'post the constructed request to the remote serever
  HttpSendRequest_Result = CBool( _
                  HttpSendRequest _
                   ( _
                     hHttpRequest:=HttpOpenRequest_hWnd, _
...

read more »



Thu, 27 Feb 2003 22:47:08 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Sending HTML forms with WINAPI

2. VBA to send Word info to HTML form?

3. How to send HTML form over internet?

4. Sending HTML Form results to a CSV file

5. Send HTML Form Data to a Server?

6. Handling file input from HTML form - sending a file to the server (IIS)

7. send html form

8. Send Report using Send Object HTML Format

9. sending HTML-Mail with images and not using ActiveDocument.send

10. Send Ipx/spx or tcp/ip data Packet with/without winapi

11. Rubberband Rectangle in ACCESS-Form via WinApi

12. HTML form data to Outlook form via mail

 

 
Powered by phpBB® Forum Software