[Visual Basic]vb调用winInet API接口post数据到指定的url

原创 2000年12月20日 02:16:00
『vb调用winInet API接口post数据到指定的url』

'This module is called modWinInet.bas. Use the SplitAddr() function to get the address in the correct format for PostInfo.

Option Explicit

'Author:    Sam Huggill
'Email:     sam@vbsquare.com

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 sHeaders 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 sHeaders As String, _
             ByVal lHeadersLength As Long, _
             ByVal lModifiers As Long) As Integer


Public Function PostInfo$(srv$, port$, script$, postdat$)

  Dim hInternetOpen As Long
  Dim hInternetConnect As Long
  Dim hHttpOpenRequest As Long
  Dim bRet As Boolean
  
  hInternetOpen = 0
  hInternetConnect = 0
  hHttpOpenRequest = 0
  
  'Use registry access settings.
  Const INTERNET_OPEN_TYPE_PRECONFIG = 0
  hInternetOpen = InternetOpen("http generic", _
                  INTERNET_OPEN_TYPE_PRECONFIG, _
                  vbNullString, _
                  vbNullString, _
                  0)
  
  If hInternetOpen <> 0 Then
     'Type of service to access.
     Const INTERNET_SERVICE_HTTP = 3
     Const INTERNET_DEFAULT_HTTP_PORT = 80
     'Change the server to your server name
     hInternetConnect = InternetConnect(hInternetOpen, _
                        srv$, _
                        port$, _
                        vbNullString, _
                        "HTTP/1.0", _
                        INTERNET_SERVICE_HTTP, _
                        0, _
                        0)
  
     If hInternetConnect <> 0 Then
      'Brings the data across the wire even if it locally cached.
       Const INTERNET_FLAG_RELOAD = &H80000000
       hHttpOpenRequest = HttpOpenRequest(hInternetConnect, _
                           "POST", _
                           script$, _
                           "HTTP/1.0", _
                           vbNullString, _
                           0, _
                           INTERNET_FLAG_RELOAD, _
                           0)
  
        If hHttpOpenRequest <> 0 Then
           Dim sHeader As String
           Const HTTP_ADDREQ_FLAG_ADD = &H20000000
           Const HTTP_ADDREQ_FLAG_REPLACE = &H80000000
  sHeader = "Content-Type: application/x-www-form-urlencoded" _
             & vbCrLf
           bRet = HttpAddRequestHeaders(hHttpOpenRequest, _
             sHeader, Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE _
             Or HTTP_ADDREQ_FLAG_ADD)
  
           Dim lpszPostData As String
           Dim lPostDataLen As Long
  
           lpszPostData = postdat$
           lPostDataLen = Len(lpszPostData)
           bRet = HttpSendRequest(hHttpOpenRequest, _
                  vbNullString, _
                  0, _
                  lpszPostData, _
                  lPostDataLen)
  
           Dim bDoLoop             As Boolean
           Dim sReadBuffer         As String * 2048
           Dim lNumberOfBytesRead  As Long
           Dim sBuffer             As String
           bDoLoop = True
           While bDoLoop
            sReadBuffer = vbNullString
            bDoLoop = InternetReadFile(hHttpOpenRequest, _
               sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
            sBuffer = sBuffer & _
                 Left(sReadBuffer, lNumberOfBytesRead)
            If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
           Wend
           PostInfo = sBuffer
           bRet = InternetCloseHandle(hHttpOpenRequest)
        End If
        bRet = InternetCloseHandle(hInternetConnect)
     End If
     bRet = InternetCloseHandle(hInternetOpen)
  End If
End Function

Public Sub SplitAddr(ByVal addr$, srv$, script$)
'Inputs: The full url including http://
' Two variables that will be changed
'
'Returns: Splits the addr$ var into the server name
' and the script path

  Dim i%

  i = InStr(addr$, "/")
  srv$ = Mid(addr$, i + 2, Len(addr$) - (i + 1))
  i = InStr(srv$, "/")
  script$ = Mid(srv$, i, Len(srv$) + 1 - i)
  srv$ = Left$(srv$, i - 1)

End Sub

版权声明:本文为博主原创文章,未经博主允许不得转载。

vb调用winInet API接口post数据到指定的url

源代码推荐:vb调用winInet API接口post数据到指定的urlThis module is called modWinInet.bas. Use the SplitAddr() functi...
  • coolstar
  • coolstar
  • 2001年05月04日 11:30
  • 2085

VB包含汉字的URL转码函数(包含空格,汉字)

测试调用的网页为aspx页面 '包含汉字的URL转码函数 Private Function vbEscape(psString As String) As String     Dim nT...
  • woolnil
  • woolnil
  • 2012年08月26日 16:09
  • 1829

VB网络编程实例大全

  • 2010年11月21日 19:41
  • 412KB
  • 下载

VB6下接口的实现

一、接口的实现1、创建一ActiveX Dll Project,命名为ICom2、添加一类(接口),命名为IDog,设置一下属性和方法:Public Property Get Name() As St...
  • gdcrx
  • gdcrx
  • 2004年08月04日 20:11
  • 623

VB调用API函数

API函数快速入门--怎样在VB中声明和使用API函数--       一、在VB中声明API函数有两种方法:如果我们只在某个窗体中使用API函数,我们可以在窗体代码的 General部分声...
  • NCTU_to_prove_safety
  • NCTU_to_prove_safety
  • 2016年07月19日 10:29
  • 598

百度翻译新版API的VB实现代码

Public Const BAIDU_APP_ID = "XXXXXXXXXXXXXXXXXXXXX" '在百度申请后得到 Public Const BAIDU_APP_KEY = "XXXXXXXX...
  • bakw
  • bakw
  • 2015年11月11日 16:05
  • 1271

VB动态调用外部API函数的方法

这么久了都没放出过什么比较好的程序出来,让大家失望了。前段时间无聊搞了个类,今天拿出来和大家分享一下。主要是实现在VB中动态调用API函数的类,才疏学浅,见笑了。Visual Basic Code**...
  • SupermanKing
  • SupermanKing
  • 2008年09月28日 14:35
  • 5309

vb调用winInet API接口post数据到指定的url

源代码推荐:vb调用winInet API接口post数据到指定的urlThis module is called modWinInet.bas. Use the SplitAddr() func...
  • hejishan
  • hejishan
  • 2008年06月08日 05:50
  • 257

VB调用API函数

API函数快速入门--怎样在VB中声明和使用API函数--       一、在VB中声明API函数有两种方法:如果我们只在某个窗体中使用API函数,我们可以在窗体代码的 General部分声明它: ...
  • lunkay
  • lunkay
  • 2016年01月13日 20:17
  • 4516

VB中API函数的调用

1、API函数       API的英文全称(Application Programming Interface),WIN32 API也就是MicrosoftWindows32位平台的应用程序编程接...
  • IstarI
  • IstarI
  • 2015年07月28日 09:53
  • 783
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[Visual Basic]vb调用winInet API接口post数据到指定的url
举报原因:
原因补充:

(最多只允许输入30个字)