This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcAsyncRequest.cls
95 lines (85 loc) · 3.13 KB
/
cAsyncRequest.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "cAsyncRequest"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private Const HTTPREQUEST_PROXYSETTING_PROXY& = 2
Public WithEvents http As WinHttp.WinHttpRequest
Attribute http.VB_VarHelpID = -1
Private mParent As cAsyncRequests, mUrl As String, mKey As String, mTimeout As Long ', mTag As String
Friend Sub Init(ParentHandler As cAsyncRequests, Key As String, Timeout As Long) ', Tag As String)
Set mParent = ParentHandler
mKey = Key
mTimeout = Timeout
'mTag = Tag
Set http = New WinHttp.WinHttpRequest
End Sub
'Public Property Let Tag(Tag As String)
'mTag = Tag
'End Property
Public Property Get Key() As String
Key = mKey
End Property
Public Property Get URL() As String
URL = mUrl
End Property
Public Sub SendRequest(URL As String, Optional PostBody As Variant, Optional AdditionalHeaders As Collection, Optional Proxy As String)
mUrl = URL
http.Option(4) = 13056
http.Option(WinHttpRequestOption_EnableRedirects) = False
http.Option(12) = True
If Proxy <> vbNullString Then http.SetProxy HTTPREQUEST_PROXYSETTING_PROXY, Proxy
If mTimeout > 0 Then http.SetTimeouts mTimeout, mTimeout, mTimeout, mTimeout
If VarType(PostBody) <> vbArray + vbByte Then
If PostBody <> Empty Then
If PostBody <> "-" And PostBody <> "@" Then
If PostBody = vbNullChar Then PostBody = vbNullString
http.Open "POST", URL, True
'If VarType(PostBody) = vbString Then If InStr(PostBody, vbNewLine) = 0 Then http.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
Else
If PostBody = "-" Then http.Open "DELETE", URL, True Else: http.Open "HEAD", URL, True
PostBody = vbNullString
End If
Else: http.Open "GET", URL, True
End If
Else: If Left$(URL, 1) = "*" Then http.Open "PUT", Mid$(URL, 2), True Else: http.Open "POST", URL, True
End If
If AdditionalHeaders.count > 0 Then
Dim i As Integer
For i = 1 To AdditionalHeaders.count
http.SetRequestHeader Split(AdditionalHeaders.Item(i), vbLf)(0), Split(AdditionalHeaders.Item(i), vbLf)(1)
Next
End If
http.Send PostBody 'send the http-request
End Sub
Public Sub AbortRequest()
If Not http Is Nothing Then http.Abort
End Sub
'Event-Delegation by direct Calls into the Parent (the aggregating cAsyncRequests-Instance)
Private Sub http_OnResponseStart(ByVal Status As Long, ByVal ContentType As String)
mParent.OnResponseStart Me, Status, ContentType
End Sub
Private Sub http_OnResponseDataAvailable(Data() As Byte)
mParent.OnResponseDataAvailable Me, Data
End Sub
Private Sub http_OnResponseFinished()
mParent.OnResponseFinished Me
End Sub
Private Sub http_OnError(ByVal ErrorNumber As Long, ByVal ErrorDescription As String)
mParent.OnError Me, ErrorNumber, ErrorDescription
End Sub
Private Sub Class_Terminate()
If Not http Is Nothing Then
http.Abort
Set http = Nothing
End If
End Sub