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

原创 2001年05月04日 11:30: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包含汉字的URL转码函数(包含空格,汉字)

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

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

『vb调用winInet API接口post数据到指定的url』This module is called modWinInet.bas. Use the SplitAddr() function t...
  • shines
  • shines
  • 2000年12月20日 02:16
  • 2492

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
  • 2074

用Wininet以Https的post方式登录

整个过程大概是这样的:发送http请求报文到服务器,报文的数据体中带有账号密码;服务器接受处理后返回响应报文;根据响应报文的状态码判断登录是否成功,通过读取返回报文的首部行获取所需的信息。我这里是通过...
  • chenxiao88957478
  • chenxiao88957478
  • 2016年07月08日 17:05
  • 1674

使用VB 以HttpPost方式 实现数据发送

Imports System.IO Imports System.Xml Imports System.Data Imports System.Text.Encoding Module Module...
  • hulele2009
  • hulele2009
  • 2013年11月30日 13:12
  • 3095

C#http post 发送json数据并含有接口的调用时间下载

  • 2017年01月05日 19:46
  • 228KB
  • 下载

向指定URL发送GET或POST请求(调第三方接口)

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
  • mr_xiyue
  • mr_xiyue
  • 2016年04月12日 16:46
  • 296

Delphi 用WinInet 单元实现 POST提交数据

由于贪方便,用idhttp控件实现POST部分。结果发现频繁提交的时候总产生10054 等N多不可控错误。遂想换其它方法。百度找了下。基本都是靠webbrowser 、idhttp等控件提交的。于是,...
  • MichaelJScofield
  • MichaelJScofield
  • 2013年04月14日 02:06
  • 9419

C#跨平台调用接口(URL传参GET调用和Post Json传参调用)

方法一:URL传参POST调用 使用HttpWebRequest,将参数POST调用   string HampsonURL = new BSysConfig().GetValue(ConfigK...
  • yitonglizihao
  • yitonglizihao
  • 2016年03月02日 15:44
  • 1274

移动GIS作业之调用百度api接口时请求中文参数url

做兴趣点搜索的时候用到了百度apistore中的api,
  • u013926110
  • u013926110
  • 2015年07月12日 20:34
  • 1157
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:vb调用winInet API接口post数据到指定的url
举报原因:
原因补充:

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