实例介绍
【实例简介】
【实例截图】
【核心代码】
Option Explicit
'Download by http://www.NewXing.com
' we set this to true whil a connection is established
Private blnConnected As Boolean
' this function sends the HTTP request
Private Sub cmdSend_Click()
Dim eUrl As URL
Dim strMethod As String
Dim strData As String
Dim strPostData As String
Dim strHeaders As String
Dim strHTTP As String
Dim X As Integer
strPostData = ""
strHeaders = ""
strMethod = cboRequestMethod.List(cboRequestMethod.ListIndex)
If blnConnected Then Exit Sub
' get the url
eUrl = ExtractUrl(txtUrl.Text)
If eUrl.Host = vbNullString Then
MsgBox "Invalid Host", vbCritical, "ERROR"
Exit Sub
End If
' configure winsock
winsock.Protocol = sckTCPProtocol
winsock.RemoteHost = eUrl.Host
If eUrl.Scheme = "http" Then
If eUrl.Port > 0 Then
winsock.RemotePort = eUrl.Port
Else
winsock.RemotePort = 80
End If
ElseIf eUrl.Scheme = vbNullString Then
winsock.RemotePort = 80
Else
MsgBox "Invalid protocol schema"
End If
' build encoded data the data is url encoded in the form
' var1=value&var2=value
strData = ""
For X = 0 To txtVariableName.Count - 1
If txtVariableName(X).Text <> vbNullString Then
strData = strData & URLEncode(txtVariableName(X).Text) & "=" & _
URLEncode(txtVariableValue(X).Text) & "&"
End If
Next X
If eUrl.Query <> vbNullString Then
eUrl.URI = eUrl.URI & "?" & eUrl.Query
End If
' check if any variables were supplied
If strData <> vbNullString Then
strData = Left(strData, Len(strData) - 1)
If strMethod = "GET" Then
' if this is a GET request then the URL encoded data
' is appended to the URI with a ?
If eUrl.Query <> vbNullString Then
eUrl.URI = eUrl.URI & "&" & strData
Else
eUrl.URI = eUrl.URI & "?" & strData
End If
Else
' if it is a post request, the data is appended to the
' body of the HTTP request and the headers Content-Type
' and Content-Length added
strPostData = strData
strHeaders = "Content-Type: application/x-www-form-urlencoded" & vbCrLf & _
"Content-Length: " & Len(strPostData) & vbCrLf
End If
End If
' get any aditional headers and add them
For X = 0 To txtHeaderName.Count - 1
If txtHeaderName(X).Text <> vbNullString Then
strHeaders = strHeaders & txtHeaderName(X).Text & ": " & _
txtHeaderValue(X).Text & vbCrLf
End If
Next X
' clear the old HTTP response
txtResponse.Text = ""
' build the HTTP request in the form
'
' {REQ METHOD} URI HTTP/1.0
' Host: {host}
' {headers}
'
' {post data}
strHTTP = strMethod & " " & eUrl.URI & " HTTP/1.0" & vbCrLf
strHTTP = strHTTP & "Host: " & eUrl.Host & vbCrLf
strHTTP = strHTTP & strHeaders
strHTTP = strHTTP & vbCrLf
strHTTP = strHTTP & strPostData
txtRequest.Text = strHTTP
winsock.Connect
' wait for a connection
While Not blnConnected
DoEvents
Wend
' send the HTTP request
winsock.SendData strHTTP
End Sub
Private Sub winsock_Connect()
blnConnected = True
End Sub
' this event occurs when data is arriving via winsock
Private Sub winsock_DataArrival(ByVal bytesTotal As Long)
Dim strResponse As String
winsock.GetData strResponse, vbString, bytesTotal
strResponse = FormatLineEndings(strResponse)
' we append this to the response box becuase data arrives
' in multiple packets
txtResponse.Text = txtResponse.Text & strResponse
End Sub
Private Sub winsock_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox Description, vbExclamation, "ERROR"
winsock.Close
End Sub
Private Sub winsock_Close()
blnConnected = False
winsock.Close
End Sub
' this function converts all line endings to Windows CrLf line endings
Private Function FormatLineEndings(ByVal str As String) As String
Dim prevChar As String
Dim nextChar As String
Dim curChar As String
Dim strRet As String
Dim X As Long
prevChar = ""
nextChar = ""
curChar = ""
strRet = ""
For X = 1 To Len(str)
prevChar = curChar
curChar = Mid$(str, X, 1)
If nextChar <> vbNullString And curChar <> nextChar Then
curChar = curChar & nextChar
nextChar = ""
ElseIf curChar = vbLf Then
If prevChar <> vbCr Then
curChar = vbCrLf
End If
nextChar = ""
ElseIf curChar = vbCr Then
nextChar = vbLf
End If
strRet = strRet & curChar
Next X
FormatLineEndings = strRet
End Function
Private Sub Form_Load()
cboRequestMethod.ListIndex = 0
blnConnected = False
End Sub
' the code below has nothing to do with winsock or HTTP and deals only with the
' display and manipulation of controls
Private Sub cmdMoreHeaders_Click()
Dim intNext As Integer
Dim lngTop As Long
' find the next control
intNext = txtHeaderName.Count
' find the next top
lngTop = txtHeaderName(intNext - 1).Top txtHeaderName(intNext - 1).Height 80
' add new controls
Load lblHeaderName(intNext)
Load txtHeaderName(intNext)
Load lblHeaderValue(intNext)
Load txtHeaderValue(intNext)
With lblHeaderName(intNext)
.Top = lngTop
.Left = lblHeaderName(intNext - 1).Left
.Visible = True
End With
With txtHeaderName(intNext)
.Top = lngTop
.Left = txtHeaderName(intNext - 1).Left
.Visible = True
.Text = ""
End With
With lblHeaderValue(intNext)
.Top = lngTop
.Left = lblHeaderValue(intNext - 1).Left
.Visible = True
End With
With txtHeaderValue(intNext)
.Top = lngTop
.Left = txtHeaderValue(intNext - 1).Left
.Visible = True
.Text = ""
End With
' set the new height of the controls container
pbxHeaders.Height = txtHeaderName(intNext).Top txtHeaderName(intNext).Height 80
' check if we should activate the scroll bar, ie: the outerbox
' is higher than the inner box
If pbxHeaders.Height > pbxOHeaders.Height Then
With vsbHeaders
.Enabled = True
.SmallChange = txtHeaderName(intNext).Height
.LargeChange = pbxOHeaders.Height
.Min = 0
.Max = pbxHeaders.Height - pbxOHeaders.Height
.Value = .Max
End With
End If
End Sub
Private Sub cmdMoreVariables_Click()
Dim intNext As Integer
Dim lngTop As Long
' find the next control
intNext = txtVariableName.Count
' find the next top
lngTop = txtVariableName(intNext - 1).Top txtVariableName(intNext - 1).Height 80
' add new controls
Load lblVariableName(intNext)
Load txtVariableName(intNext)
Load lblVariableValue(intNext)
Load txtVariableValue(intNext)
With lblVariableName(intNext)
.Top = lngTop
.Left = lblVariableName(intNext - 1).Left
.Visible = True
End With
With txtVariableName(intNext)
.Top = lngTop
.Left = txtVariableName(intNext - 1).Left
.Visible = True
.TabIndex = txtVariableName(intNext - 1).TabIndex 2
.Text = ""
End With
With lblVariableValue(intNext)
.Top = lngTop
.Left = lblVariableValue(intNext - 1).Left
.Visible = True
End With
With txtVariableValue(intNext)
.Top = lngTop
.Left = txtVariableValue(intNext - 1).Left
.TabIndex = txtVariableValue(intNext - 1).TabIndex 2
.Visible = True
.Text = ""
End With
' set the new height of the controls container
pbxVariables.Height = txtVariableName(intNext).Top txtVariableName(intNext).Height 80
' check if we should activate the scroll bar, ie: the outerbox
' is higher than the inner box
If pbxVariables.Height > pbxOVariables.Height Then
With vsbVariables
.Enabled = True
.SmallChange = txtVariableName(intNext).Height
.LargeChange = pbxOVariables.Height
.Min = 0
.Max = pbxVariables.Height - pbxOVariables.Height
.Value = .Max
End With
End If
End Sub
Private Sub vsbHeaders_Change()
pbxHeaders.Top = 0 - vsbHeaders.Value
End Sub
Private Sub vsbHeaders_Scroll()
pbxHeaders.Top = 0 - vsbHeaders.Value
End Sub
Private Sub vsbVariables_Change()
pbxVariables.Top = 0 - vsbVariables.Value
End Sub
Private Sub vsbVariables_Scroll()
pbxVariables.Top = 0 - vsbVariables.Value
End Sub
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论