分享
 
 
 

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

王朝vb·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

源代码推荐: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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有