-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathWinHttpWebSocketSample.bas
258 lines (220 loc) · 7.62 KB
/
WinHttpWebSocketSample.bas
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
Attribute VB_Name = "WinHttpWebSocketSample"
Option Explicit
' original sample code, pulled from:
' https://github.com/microsoft/Windows-classic-samples/blob/master/Samples/WinhttpWebsocket/cpp/WinhttpWebsocket.cpp
'// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
'// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'// PARTICULAR PURPOSE.
'//
'// Copyright (c) Microsoft Corporation. All rights reserved
'#include <Windows.h>
'#include <WinHttp.h>
'#include <stdio.h>
'int __cdecl wmain()
'{
' DWORD dwError = ERROR_SUCCESS;
' BOOL fStatus = FALSE;
' HINTERNET hSessionHandle = NULL;
' HINTERNET hConnectionHandle = NULL;
' HINTERNET hRequestHandle = NULL;
' HINTERNET hWebSocketHandle = NULL;
' BYTE rgbCloseReasonBuffer[123];
' BYTE rgbBuffer[1024];
' BYTE *pbCurrentBufferPointer = rgbBuffer;
' DWORD dwBufferLength = ARRAYSIZE(rgbBuffer);
' DWORD dwBytesTransferred = 0;
' DWORD dwCloseReasonLength = 0;
' USHORT usStatus = 0;
' WINHTTP_WEB_SOCKET_BUFFER_TYPE eBufferType;
' INTERNET_PORT Port = INTERNET_DEFAULT_HTTP_PORT;
' const WCHAR *pcwszServerName = L"localhost";
' const WCHAR *pcwszPath = L"/WinHttpWebSocketSample/EchoWebSocket.ashx";
' const WCHAR *pcwszMessage = L"Hello world";
' const DWORD cdwMessageLength = ARRAYSIZE(L"Hello world") * sizeof(WCHAR);
' //
' // Create session, connection and request handles.
' //
' hSessionHandle = WinHttpOpen(L"WebSocket sample",
' WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
' NULL,
' NULL,
' 0);
' if (hSessionHandle == NULL)
' {
' dwError = GetLastError();
' goto quit;
' }
' hConnectionHandle = WinHttpConnect(hSessionHandle,
' pcwszServerName,
' Port,
' 0);
' if (hConnectionHandle == NULL)
' {
' dwError = GetLastError();
' goto quit;
' }
' hRequestHandle = WinHttpOpenRequest(hConnectionHandle,
' L"GET",
' pcwszPath,
' NULL,
' NULL,
' NULL,
' 0);
' if (hRequestHandle == NULL)
' {
' dwError = GetLastError();
' goto quit;
' }
' //
' // Request protocol upgrade from http to websocket.
' //
'#pragma prefast(suppress:6387, "WINHTTP_OPTION_UPGRADE_TO_WEB_SOCKET does not take any arguments.")
' fStatus = WinHttpSetOption(hRequestHandle,
' WINHTTP_OPTION_UPGRADE_TO_WEB_SOCKET,
' NULL,
' 0);
' if (!fStatus)
' {
' dwError = GetLastError();
' goto quit;
' }
' //
' // Perform websocket handshake by sending a request and receiving server's response.
' // Application may specify additional headers if needed.
' //
' fStatus = WinHttpSendRequest(hRequestHandle,
' WINHTTP_NO_ADDITIONAL_HEADERS,
' 0,
' NULL,
' 0,
' 0,
' 0);
' if (!fStatus)
' {
' dwError = GetLastError();
' goto quit;
' }
' fStatus = WinHttpReceiveResponse(hRequestHandle, 0);
' if (!fStatus)
' {
' dwError = GetLastError();
' goto quit;
' }
' //
' // Application should check what is the HTTP status code returned by the server and behave accordingly.
' // WinHttpWebSocketCompleteUpgrade will fail if the HTTP status code is different than 101.
' //
' hWebSocketHandle = WinHttpWebSocketCompleteUpgrade(hRequestHandle, NULL);
' if (hWebSocketHandle == NULL)
' {
' dwError = GetLastError();
' goto quit;
' }
' //
' // The request handle is not needed anymore. From now on we will use the websocket handle.
' //
' WinHttpCloseHandle(hRequestHandle);
' hRequestHandle = NULL;
' wprintf(L"Succesfully upgraded to websocket protocol\n");
' //
' // Send and receive data on the websocket protocol.
' //
' dwError = WinHttpWebSocketSend(hWebSocketHandle,
' WINHTTP_WEB_SOCKET_BINARY_MESSAGE_BUFFER_TYPE,
' (PVOID)pcwszMessage,
' cdwMessageLength);
' if (dwError != ERROR_SUCCESS)
' {
' goto quit;
' }
' wprintf(L"Sent message to the server: '%s'\n", pcwszMessage);
' Do
' {
' if (dwBufferLength == 0)
' {
' dwError = ERROR_NOT_ENOUGH_MEMORY;
' goto quit;
' }
' dwError = WinHttpWebSocketReceive(hWebSocketHandle,
' pbCurrentBufferPointer,
' dwBufferLength,
' &dwBytesTransferred,
' &eBufferType);
' if (dwError != ERROR_SUCCESS)
' {
' goto quit;
' }
' //
' // If we receive just part of the message restart the receive operation.
' //
' pbCurrentBufferPointer += dwBytesTransferred;
' dwBufferLength -= dwBytesTransferred;
' }
' while (eBufferType == WINHTTP_WEB_SOCKET_BINARY_FRAGMENT_BUFFER_TYPE);
' //
' // We expected server just to echo single binary message.
' //
' if (eBufferType != WINHTTP_WEB_SOCKET_BINARY_MESSAGE_BUFFER_TYPE)
' {
' wprintf(L"Unexpected buffer type\n");
' dwError = ERROR_INVALID_PARAMETER;
' goto quit;
' }
' wprintf(L"Received message from the server: '%.*s'\n", dwBufferLength, (WCHAR*)rgbBuffer);
' //
' // Gracefully close the connection.
' //
' dwError = WinHttpWebSocketClose(hWebSocketHandle,
' WINHTTP_WEB_SOCKET_SUCCESS_CLOSE_STATUS,
' NULL,
' 0);
' if (dwError != ERROR_SUCCESS)
' {
' goto quit;
' }
' //
' // Check close status returned by the server.
' //
' dwError = WinHttpWebSocketQueryCloseStatus(hWebSocketHandle,
' &usStatus,
' rgbCloseReasonBuffer,
' ARRAYSIZE(rgbCloseReasonBuffer),
' &dwCloseReasonLength);
' if (dwError != ERROR_SUCCESS)
' {
' goto quit;
' }
' wprintf(L"The server closed the connection with status code: '%d' and reason: '%.*S'\n",
' (int)usStatus,
' dwCloseReasonLength,
' rgbCloseReasonBuffer);
'quit:
' if (hRequestHandle != NULL)
' {
' WinHttpCloseHandle(hRequestHandle);
' hRequestHandle = NULL;
' }
'
' if (hWebSocketHandle != NULL)
' {
' WinHttpCloseHandle(hWebSocketHandle);
' hWebSocketHandle = NULL;
' }
' if (hConnectionHandle != NULL)
' {
' WinHttpCloseHandle(hConnectionHandle);
' hConnectionHandle = NULL;
' }
' if (hSessionHandle != NULL)
' {
' WinHttpCloseHandle(hSessionHandle);
' hSessionHandle = NULL;
' }
' if (dwError != ERROR_SUCCESS)
' {
' wprintf(L"Application failed with error: %u\n", dwError);
' return -1;
' }
' return 0;
'}