-
Notifications
You must be signed in to change notification settings - Fork 5
/
AsyncDemo.cpp
1087 lines (935 loc) · 42.9 KB
/
AsyncDemo.cpp
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************
* *
* AsyncDemo.cpp *
* *
* Copyright (c) Microsoft Corporation. All Rights Reserved. *
* *
************************************************************/
#define UNICODE
#include <windows.h>
#include <winhttp.h>
#include <stdio.h>
#include "resource.h"
//********************************************************************
// Global Variables
//********************************************************************
// Context value structure.
typedef struct {
HWND hWindow; // Handle for the dialog box
HINTERNET hConnect; // Connection handle
HINTERNET hRequest; // Resource request handle
int nURL; // ID of the URL edit box
int nHeader; // ID of the header output box
int nResource; // ID of the resource output box
DWORD dwSize; // Size of the latest data block
DWORD dwTotalSize; // Size of the total data
LPSTR lpBuffer; // Buffer for storing read data
WCHAR szMemo[256]; // String providing state information
} REQUEST_CONTEXT;
// Two Instances of the context value structure.
static REQUEST_CONTEXT rcContext, rcContext2;
// Session handle.
HINTERNET hSession;
WINHTTP_STATUS_CALLBACK pCallback = NULL;
CRITICAL_SECTION g_CallBackCritSec;
//
// maximum field lengths (arbitrary) from wininet.h
//
#define INTERNET_MAX_HOST_NAME_LENGTH 256
#define INTERNET_MAX_USER_NAME_LENGTH 128
#define INTERNET_MAX_PASSWORD_LENGTH 128
#define INTERNET_MAX_PORT_NUMBER_LENGTH 5 // INTERNET_PORT is unsigned short
#define INTERNET_MAX_PORT_NUMBER_VALUE 65535 // maximum unsigned short value
#define INTERNET_MAX_PATH_LENGTH 2048
#define INTERNET_MAX_SCHEME_LENGTH 32 // longest protocol name length
#define INTERNET_MAX_URL_LENGTH (INTERNET_MAX_SCHEME_LENGTH \
+ sizeof("://") \
+ INTERNET_MAX_PATH_LENGTH)
BOOL CALLBACK AsyncDialog(HWND hX, UINT message, WPARAM wParam, LPARAM lParam);
BOOL SendRequest(REQUEST_CONTEXT* cpContext, LPWSTR szURL);
// Forward declaration.
void __stdcall AsyncCallback(HINTERNET, DWORD_PTR, DWORD, LPVOID, DWORD);
void Cleanup(REQUEST_CONTEXT* cpContext);
BOOL bAutomaticProxyConfiguration = FALSE;
BOOL bForceTLS13 = FALSE;
HWND hDialog = NULL;
//********************************************************************
// Main Program
//********************************************************************
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
LPSTR lpszArgs, int nWinMode)
{
InitializeCriticalSection(&g_CallBackCritSec);
// Show the dialog box.
DialogBox(hThisInst, MAKEINTRESOURCE(IDD_DIALOG1),
HWND_DESKTOP, (DLGPROC)AsyncDialog);
DeleteCriticalSection(&g_CallBackCritSec);
return 0;
}
//********************************************************************
// Dialog Function
//********************************************************************
BOOL CALLBACK AsyncDialog(HWND hX, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
hDialog = hX;
// Set the default web sites.
SetDlgItemText(hX, IDC_URL1, L"http://www.bing.com");
SendDlgItemMessage(hDialog, IDC_AUTOMATIC_PROXY_DETECTION, BM_SETCHECK, BST_CHECKED, 0);
// Initialize the first context value.
rcContext.hWindow = hX;
rcContext.nURL = IDC_URL1;
rcContext.nHeader = IDC_HEADER1;
rcContext.nResource = IDC_RESOURCE1;
rcContext.hConnect = 0;
rcContext.hRequest = 0;
rcContext.lpBuffer = NULL;
rcContext.szMemo[0] = 0;
return TRUE;
case WM_CLOSE:
// Close the session handle.
WCHAR szBuffer[256];
swprintf(szBuffer, sizeof(szBuffer), L"->WinHttCloseHandle hSession: %X", (unsigned int)hSession);
SendDlgItemMessage(rcContext.hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
WinHttpCloseHandle(hSession);
EndDialog(hX, 0);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_EXIT:
EndDialog(hX, 0);
return TRUE;
case IDC_DOWNLOAD:
WCHAR szURL[INTERNET_MAX_URL_LENGTH];
// Disable the download button.
EnableWindow(GetDlgItem(hX, IDC_DOWNLOAD), 0);
// Reset the edit boxes.
SetDlgItemText(hX, IDC_HEADER1, NULL);
SetDlgItemText(hX, IDC_RESOURCE1, NULL);
SendDlgItemMessage(hX, IDC_CBLIST, LB_RESETCONTENT, 0, NULL);
// Obtain the URLs from the dialog box and send the request.
GetDlgItemText(hX, IDC_URL1, szURL, INTERNET_MAX_URL_LENGTH);
BOOL fRequest = SendRequest(&rcContext, szURL);
// Enable the download button if both request are failing.
EnableWindow(GetDlgItem(hX, IDC_DOWNLOAD), TRUE);
return (fRequest);
}
default:
return FALSE;
}
}
//********************************************************************
// Error Messages
//********************************************************************
// This macro returns the constant name in a string.
#define CASE_OF(constant) case constant: return (L# constant)
LPCWSTR GetApiErrorString(DWORD dwResult)
{
// Return the error result as a string so that the
// name of the function causing the error can be displayed.
switch(dwResult)
{
CASE_OF( API_RECEIVE_RESPONSE );
CASE_OF( API_QUERY_DATA_AVAILABLE );
CASE_OF( API_READ_DATA );
CASE_OF( API_WRITE_DATA );
CASE_OF( API_SEND_REQUEST );
}
return L"Unknown function";
}
BOOL SendRequest(REQUEST_CONTEXT *cpContext, LPWSTR szURL)
{
WCHAR szHost[256];
DWORD dwOpenRequestFlag = 0;
URL_COMPONENTS urlComp;
BOOL fRet = FALSE;
WCHAR szBuffer[256];
WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions = { 0 };
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG IEProxyConfig;
WINHTTP_PROXY_INFO proxyInfo = { 0 };
// Initialize URL_COMPONENTS structure.
ZeroMemory(&urlComp, sizeof(urlComp));
urlComp.dwStructSize = sizeof(urlComp);
// Use allocated buffer to store the Host Name.
urlComp.lpszHostName = szHost;
urlComp.dwHostNameLength = sizeof(szHost) / sizeof(szHost[0]);
// Set non zero lengths to obtain pointer to the URL Path.
/* note: if we threat this pointer as a NULL terminated string
this pointer will contain Extra Info as well. */
urlComp.dwUrlPathLength = -1;
// Crack HTTP scheme.
urlComp.dwSchemeLength = -1;
DWORD dwAccessType = 0;
//checking for automatic proxy detection set
if (SendDlgItemMessage(hDialog, IDC_AUTOMATIC_PROXY_DETECTION, BM_GETCHECK, 0, 0))
{
dwAccessType = WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY;
bAutomaticProxyConfiguration = TRUE;
swprintf(szBuffer, sizeof(szBuffer), L"->WinHttpOpen with WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY access type");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
else
{
dwAccessType = WINHTTP_ACCESS_TYPE_DEFAULT_PROXY;
bAutomaticProxyConfiguration = FALSE;
swprintf(szBuffer, sizeof(szBuffer), L"->WinHttpOpen WINHTTP_ACCESS_TYPE_DEFAULT_PROXY access type");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
// Create the session handle using the default settings.
hSession = WinHttpOpen(L"Asynchronous WinHTTP Demo/1.1",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
WINHTTP_FLAG_ASYNC);
swprintf(szBuffer, sizeof(szBuffer), L"->WinHttpOpen WINHTTP_ACCESS_TYPE_DEFAULT_PROXY access type");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
// Check to see if the session handle was successfully created.
if (hSession == NULL)
{
swprintf(szBuffer, sizeof(szBuffer), L"<-- WinHttpOpen failed : %X", GetLastError());
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
goto cleanup;
}
swprintf(szBuffer, sizeof(szBuffer), L"<-- WinHttpOpen success. hSession : %X", (unsigned int)hSession);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
swprintf( szBuffer, sizeof(szBuffer), L"->Calling WinHttpCrackURL for %s", szURL);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
// Crack the URL.
if (!WinHttpCrackUrl(szURL, 0, 0, &urlComp))
{
swprintf(szBuffer, sizeof(szBuffer), L"<-- WinHttpCrackUrl failed : %X", GetLastError());
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
goto cleanup;
}
// Install the status callback function.
if (pCallback == NULL)
{
swprintf(szBuffer, sizeof(szBuffer), L"->Calling WinHttpSetStatusCallback with WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS. hSession: %X", (unsigned int)hSession);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
pCallback = WinHttpSetStatusCallback(hSession,
(WINHTTP_STATUS_CALLBACK)AsyncCallback,
WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,
NULL);
}
// note: On success WinHttpSetStatusCallback returns the previously defined callback function.
// Here it should be NULL
if (pCallback == WINHTTP_INVALID_STATUS_CALLBACK)
{
swprintf(szBuffer, sizeof(szBuffer), L"<-WinHttpSetStatusCallback WINHTTP_INVALID_STATUS_CALLBACK");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
goto cleanup;
}
swprintf(szBuffer, sizeof(szBuffer), L"<-WinHttpSetStatusCallback succeeded");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
if (SendDlgItemMessage(hDialog, IDC_TLS_13, BM_GETCHECK, 0, 0))
{
DWORD dwFlags = WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3;
WinHttpSetOption(hSession, WINHTTP_OPTION_SECURE_PROTOCOLS, &dwFlags, sizeof(dwFlags));
}
swprintf(szBuffer, sizeof(szBuffer), L"->Calling WinHttpConnect for host %s and port %d", szHost, urlComp.nPort);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
// Open an HTTP session.
cpContext->hConnect = WinHttpConnect(hSession, szHost,
urlComp.nPort, 0);
if (NULL == cpContext->hConnect)
{
swprintf(szBuffer, sizeof(szBuffer), L"<-WinHttpConnect failed : %X", GetLastError());
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
goto cleanup;
}
swprintf(szBuffer, sizeof(szBuffer), L"<-WinHttpConnect succeeded. hConnect = %X", (unsigned int)cpContext->hConnect);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
if (bAutomaticProxyConfiguration)
{
swprintf(szBuffer, sizeof(szBuffer), L"->Calling WinHttpGetIEProxyConfigForCurrentUser");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
if (WinHttpGetIEProxyConfigForCurrentUser(&IEProxyConfig))
{
swprintf(szBuffer, sizeof(szBuffer), L"<-WinHttpGetIEProxyConfigForCurrentUser succeeded");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
//
// If IE is configured to autodetect, then we'll autodetect too
//
if (IEProxyConfig.fAutoDetect)
{
swprintf(szBuffer, sizeof(szBuffer), L"Automatically detect settings set");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
//
// Use both DHCP and DNS-based autodetection
//
AutoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP |
WINHTTP_AUTO_DETECT_TYPE_DNS_A;
}
//
// If there's an autoconfig URL stored in the IE proxy settings, save it
//
if (IEProxyConfig.lpszAutoConfigUrl)
{
swprintf(szBuffer, sizeof(szBuffer), L"Autoconfiguration url set to : %s", IEProxyConfig.lpszAutoConfigUrl);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_CONFIG_URL;
AutoProxyOptions.lpszAutoConfigUrl = IEProxyConfig.lpszAutoConfigUrl;
}
//
// If there's a static proxy
//
if (IEProxyConfig.lpszProxy)
{
swprintf(szBuffer, sizeof(szBuffer), L"Static proxy set to : %s", IEProxyConfig.lpszProxy);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
AutoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_ALLOW_STATIC;
}
swprintf(szBuffer, sizeof(szBuffer), L"->Calling WinHttpGetProxyForUrl");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
BOOL bResult = WinHttpGetProxyForUrl(hSession,
urlComp.lpszScheme,
&AutoProxyOptions,
&proxyInfo);
DWORD dwError;
if (!bResult)
{
dwError = GetLastError();
swprintf(szBuffer, sizeof(szBuffer), L"<-WinHttpGetProxyForUrl failed : %X", dwError);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"<-- WinHttpGetProxyForUrl success");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
if (proxyInfo.lpszProxy)
{
swprintf(szBuffer, sizeof(szBuffer), L"Proxy :%s", proxyInfo.lpszProxy);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
if (proxyInfo.lpszProxyBypass)
{
swprintf(szBuffer, sizeof(szBuffer), L"Proxy bypass :%s", proxyInfo.lpszProxyBypass);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
/*
// WinHttpOpen dwAccessType values (also for WINHTTP_PROXY_INFO::dwAccessType)
#define WINHTTP_ACCESS_TYPE_DEFAULT_PROXY 0
#define WINHTTP_ACCESS_TYPE_NO_PROXY 1
#define WINHTTP_ACCESS_TYPE_NAMED_PROXY 3
*/
printf("\tAccessType : %d\r\n", proxyInfo.dwAccessType);
swprintf(szBuffer, sizeof(szBuffer), L"dwAccessType : %d", proxyInfo.dwAccessType);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
if (proxyInfo.dwAccessType == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
{
swprintf(szBuffer, sizeof(szBuffer), L"WINHTTP_ACCESS_TYPE_DEFAULT_PROXY");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
if (proxyInfo.dwAccessType == WINHTTP_ACCESS_TYPE_NO_PROXY)
{
swprintf(szBuffer, sizeof(szBuffer), L"WINHTTP_ACCESS_TYPE_NO_PROXY");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
if (proxyInfo.dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
{
swprintf(szBuffer, sizeof(szBuffer), L"WINHTTP_ACCESS_TYPE_NAMED_PROXY");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
swprintf(szBuffer, sizeof(szBuffer), L"->Calling WinHttpSetOption WINHTTP_OPTION_PROXY");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
if (!WinHttpSetOption(hSession,
WINHTTP_OPTION_PROXY,
&proxyInfo,
sizeof(proxyInfo)))
{
dwError = GetLastError();
swprintf(szBuffer, sizeof(szBuffer), L"<-- WinHttpSetOption WINHTTP_OPTION_PROXY failed : %X", dwError);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
else
{
dwError = GetLastError();
swprintf(szBuffer, sizeof(szBuffer), L"<-- WinHttpSetOption WINHTTP_OPTION_PROXY success");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
}
}
}
// Prepare OpenRequest flag
dwOpenRequestFlag = (INTERNET_SCHEME_HTTPS == urlComp.nScheme) ?
WINHTTP_FLAG_SECURE : 0;
swprintf(szBuffer, sizeof(szBuffer), L"->Calling WinHttpOpenRequest");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
// Open a "GET" request.
cpContext->hRequest = WinHttpOpenRequest(cpContext->hConnect,
L"GET", urlComp.lpszUrlPath,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
dwOpenRequestFlag);
if (cpContext->hRequest == 0)
{
swprintf(szBuffer, sizeof(szBuffer), L"<-WinHttpOpenRequest failed : %X", GetLastError());
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
goto cleanup;
}
swprintf(szBuffer, sizeof(szBuffer), L"<-WinHttpOpenRequest succeeded. hRequest : %X", (unsigned int)cpContext->hRequest);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
WINHTTP_PROXY_INFO ProxyInfo;
DWORD cbProxyInfoSize = sizeof(ProxyInfo);
printf("\t-\n");
swprintf(szBuffer, sizeof(szBuffer), L"->WinHttpQueryOption with WINHTTP_OPTION_PROXY");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
if (!WinHttpQueryOption(cpContext->hRequest,
WINHTTP_OPTION_PROXY,
&ProxyInfo,
&cbProxyInfoSize))
{
// Exit if setting the proxy info failed.
swprintf(szBuffer, sizeof(szBuffer), L"<-WinHttpQueryOption WINHTTP_OPTION_PROXY failed : %X", GetLastError());
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"<-WinHttpQueryOption WINHTTP_OPTION_PROXY suceeded");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
swprintf(szBuffer, sizeof(szBuffer), L"Proxy : %s", ProxyInfo.lpszProxy);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
swprintf(szBuffer, sizeof(szBuffer), L"ProxyBypass : %s", ProxyInfo.lpszProxyBypass);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
/*
// WinHttpOpen dwAccessType values (also for WINHTTP_PROXY_INFO::dwAccessType)
#define WINHTTP_ACCESS_TYPE_DEFAULT_PROXY 0
#define WINHTTP_ACCESS_TYPE_NO_PROXY 1
#define WINHTTP_ACCESS_TYPE_NAMED_PROXY 3
*/
printf("\tAccessType : %d\r\n", ProxyInfo.dwAccessType);
swprintf(szBuffer, sizeof(szBuffer), L"AccessType : %d", ProxyInfo.dwAccessType);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
if (ProxyInfo.dwAccessType == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
{
swprintf(szBuffer, sizeof(szBuffer), L"WINHTTP_ACCESS_TYPE_DEFAULT_PROXY");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
if (ProxyInfo.dwAccessType == WINHTTP_ACCESS_TYPE_NO_PROXY)
{
swprintf(szBuffer, sizeof(szBuffer), L"WINHTTP_ACCESS_TYPE_NO_PROXY");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
if (ProxyInfo.dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
{
swprintf(szBuffer, sizeof(szBuffer), L"WINHTTP_ACCESS_TYPE_NAMED_PROXY");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
}
}
swprintf( szBuffer, sizeof(szBuffer), L"->Calling WinHttpSendRequest. hRequest : %X", (unsigned int)cpContext->hRequest);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
// Send the request.
if (!WinHttpSendRequest(cpContext->hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0, 0,
(DWORD_PTR)cpContext))
{
swprintf(szBuffer, sizeof(szBuffer), L"<-WinHttpSendRequest failed : %X", GetLastError());
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
goto cleanup;
}
swprintf(szBuffer, sizeof(szBuffer), L"<-WinHttpSendRequest succeeded");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
fRet = TRUE;
cleanup:
if (fRet == FALSE)
{
WCHAR szError[256];
// Set the error message.
swprintf(szError, sizeof(szBuffer), L"%s failed with error %d", szBuffer, GetLastError());
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
// Cleanup handles.
Cleanup(cpContext);
// Display the error message.
SetDlgItemText(cpContext->hWindow, cpContext->nResource, szError);
// Close the session handle.
swprintf(szBuffer, sizeof(szBuffer), L"->WinHttCloseHandle hSession: %X", (unsigned int)hSession);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
WinHttpCloseHandle(hSession);
}
return fRet;
}
//********************************************************************
// Additional Functions
//********************************************************************
void Cleanup(REQUEST_CONTEXT* cpContext)
{
WCHAR szBuffer[256];
// Set the memo to indicate a closed handle.
swprintf(szBuffer, sizeof(szBuffer), L"Cleanup");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
if (cpContext->hRequest)
{
swprintf(szBuffer, sizeof(szBuffer), L"->WinHttpSetStatusCallback NULL");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
WinHttpSetStatusCallback(cpContext->hRequest,
NULL,
NULL,
NULL);
swprintf(szBuffer, sizeof(szBuffer), L"<--WinHttpSetStatusCallback NULL");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
swprintf(szBuffer, sizeof(szBuffer), L"->WinHttpCloseHandle hRequest (%X)", (unsigned int)cpContext->hRequest);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
WinHttpCloseHandle(cpContext->hRequest);
swprintf(szBuffer, sizeof(szBuffer), L"<--WinHttpCloseHandle");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
cpContext->hRequest = NULL;
}
if (cpContext->hConnect)
{
swprintf(szBuffer, sizeof(szBuffer), L"->WinHttpCloseHandle hConnect (%X)", (unsigned int)cpContext->hConnect);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
WinHttpCloseHandle(cpContext->hConnect);
swprintf(szBuffer, sizeof(szBuffer), L"<--WinHttpCloseHandle");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
cpContext->hConnect = NULL;
}
delete[] cpContext->lpBuffer;
cpContext->lpBuffer = NULL;
// note: this function can be called concurrently by differnet threads, therefore any global data
// reference needs to be protected
EnterCriticalSection(&g_CallBackCritSec);
//Re-enable the download button.
EnableWindow(GetDlgItem(cpContext->hWindow, IDC_DOWNLOAD), 1);
LeaveCriticalSection(&g_CallBackCritSec);
}
BOOL Header(REQUEST_CONTEXT *cpContext)
{
DWORD dwSize=0;
LPVOID lpOutBuffer = NULL;
WCHAR szBuffer[256];
// Set the state memo.
swprintf(szBuffer, sizeof(szBuffer), L"->Calling WinHttpQueryHeaders");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
// Use HttpQueryInfo to obtain the size of the buffer.
if (!WinHttpQueryHeaders( cpContext->hRequest,
WINHTTP_QUERY_RAW_HEADERS_CRLF,
WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX))
{
// An ERROR_INSUFFICIENT_BUFFER is expected because you
// are looking for the size of the headers. If any other
// error is encountered, display error information.
DWORD dwErr = GetLastError();
if (dwErr != ERROR_INSUFFICIENT_BUFFER)
{
swprintf( szBuffer, sizeof(szBuffer), L"Error %d encountered.", dwErr);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
SetDlgItemText( cpContext->hWindow, cpContext->nResource, szBuffer);
return FALSE;
}
}
// Allocate memory for the buffer.
lpOutBuffer = new WCHAR[dwSize];
// Use HttpQueryInfo to obtain the header buffer.
if(WinHttpQueryHeaders( cpContext->hRequest,
WINHTTP_QUERY_RAW_HEADERS_CRLF,
WINHTTP_HEADER_NAME_BY_INDEX, lpOutBuffer, &dwSize, WINHTTP_NO_HEADER_INDEX))
SetDlgItemText( cpContext->hWindow, cpContext->nHeader,
(LPWSTR)lpOutBuffer);
// Free the allocated memory.
delete [] lpOutBuffer;
return TRUE;
}
BOOL QueryData(REQUEST_CONTEXT *cpContext)
{
WCHAR szBuffer[256];
// Set the state memo.
swprintf( szBuffer, sizeof(szBuffer), L"->Calling WinHttpQueryDataAvailable");
// Chech for available data.
if (WinHttpQueryDataAvailable(cpContext->hRequest, NULL) == FALSE)
{
// If a synchronous error occured, display the error. Otherwise
// the query is successful or asynchronous.
DWORD dwErr = GetLastError();
swprintf( szBuffer, sizeof(szBuffer), L"Error %d encountered.", dwErr);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
SetDlgItemText( cpContext->hWindow, cpContext->nResource, szBuffer);
return FALSE;
}
return TRUE;
}
void TransferAndDeleteBuffers(REQUEST_CONTEXT *cpContext, LPSTR lpReadBuffer, DWORD dwBytesRead)
{
cpContext->dwSize = dwBytesRead;
if(!cpContext->lpBuffer)
{
// If there is no context buffer, start one with the read data.
cpContext->lpBuffer = lpReadBuffer;
}
else
{
// Store the previous buffer, and create a new one big
// enough to hold the old data and the new data.
LPSTR lpOldBuffer = cpContext->lpBuffer;
cpContext->lpBuffer = new char[cpContext->dwTotalSize + cpContext->dwSize];
// Copy the old and read buffer into the new context buffer.
memcpy(cpContext->lpBuffer, lpOldBuffer, cpContext->dwTotalSize);
memcpy(cpContext->lpBuffer + cpContext->dwTotalSize, lpReadBuffer, cpContext->dwSize);
// Free the memory allocated to the old and read buffers.
delete [] lpOldBuffer;
delete [] lpReadBuffer;
}
// Keep track of the total size.
cpContext->dwTotalSize += cpContext->dwSize;
}
BOOL ReadData(REQUEST_CONTEXT *cpContext)
{
LPSTR lpOutBuffer = new char[cpContext->dwSize+1];
WCHAR szBuffer[256];
ZeroMemory(lpOutBuffer, cpContext->dwSize+1);
// Set the state memo.
swprintf( szBuffer, sizeof(szBuffer), L"->Calling WinHttpReadData with size %d", cpContext->dwSize);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
// Read the available data.
if (WinHttpReadData( cpContext->hRequest, (LPVOID)lpOutBuffer,
cpContext->dwSize, NULL) == FALSE)
{
// If a synchronous error occurred, display the error. Otherwise
// the read is successful or asynchronous.
DWORD dwErr = GetLastError();
swprintf( szBuffer, sizeof(szBuffer), L"WinHttpReadData Error %d encountered.", dwErr);
SetDlgItemText( cpContext->hWindow, cpContext->nResource, szBuffer);
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
delete [] lpOutBuffer;
return FALSE;
}
swprintf(szBuffer, sizeof(szBuffer), L"<--WinHttpReadData");
SendDlgItemMessage(cpContext->hWindow, IDC_CBLIST, LB_ADDSTRING, 0, (LPARAM)szBuffer);
return TRUE;
}
//********************************************************************
// Status Callback
//********************************************************************
void __stdcall AsyncCallback( HINTERNET hInternet, DWORD_PTR dwContext,
DWORD dwInternetStatus,
LPVOID lpvStatusInformation,
DWORD dwStatusInformationLength)
{
REQUEST_CONTEXT *cpContext;
WCHAR szBuffer[1024];
cpContext = (REQUEST_CONTEXT*)dwContext;
WINHTTP_ASYNC_RESULT *pAR;
if (cpContext == NULL)
{
// this should not happen, but we are being defensive here
return;
}
szBuffer[0] = 0;
// Create a string that reflects the status flag.
switch (dwInternetStatus)
{
case WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION:
//Closing the connection to the server.The lpvStatusInformation parameter is NULL.
swprintf(szBuffer, sizeof(szBuffer), L"CLOSING_CONNECTION (%d)", dwStatusInformationLength);
break;
case WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER:
//Successfully connected to the server.
//The lpvStatusInformation parameter contains a pointer to an LPWSTR that indicates the IP address of the server in dotted notation.
if (lpvStatusInformation)
{
swprintf(szBuffer, sizeof(szBuffer), L"CONNECTED_TO_SERVER (%s)", (WCHAR *)lpvStatusInformation);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"CONNECTED_TO_SERVER (%d)", dwStatusInformationLength);
}
break;
case WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER:
//Connecting to the server.
//The lpvStatusInformation parameter contains a pointer to an LPWSTR that indicates the IP address of the server in dotted notation.
if (lpvStatusInformation)
{
swprintf(szBuffer, sizeof(szBuffer), L"CONNECTING_TO_SERVER (%s)", (WCHAR *)lpvStatusInformation);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"CONNECTING_TO_SERVER (%d)", dwStatusInformationLength);
}
break;
case WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED:
//Successfully closed the connection to the server. The lpvStatusInformation parameter is NULL.
swprintf(szBuffer, sizeof(szBuffer), L"CONNECTION_CLOSED (%d)", dwStatusInformationLength);
break;
case WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE:
//Data is available to be retrieved with WinHttpReadData.The lpvStatusInformation parameter points to a DWORD that contains the number of bytes of data available.
//The dwStatusInformationLength parameter itself is 4 (the size of a DWORD).
cpContext->dwSize = *((LPDWORD)lpvStatusInformation);
// If there is no data, the process is complete.
if (cpContext->dwSize == 0)
{
swprintf(szBuffer, sizeof(szBuffer), L"DATA_AVAILABLE Number of bytes available : %d. All data has been read -> Displaying the data.", cpContext->dwSize);
// All of the data has been read. Display the data.
if (cpContext->dwTotalSize)
{
// Convert the final context buffer to wide characters,
// and display.
LPWSTR lpWideBuffer = new WCHAR[cpContext->dwTotalSize + 1];
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
cpContext->lpBuffer,
cpContext->dwTotalSize,
lpWideBuffer,
cpContext->dwTotalSize);
lpWideBuffer[cpContext->dwTotalSize] = 0;
/* note: in the case of binary data, only data upto the first null will be displayed */
SetDlgItemText(cpContext->hWindow, cpContext->nResource, lpWideBuffer);
// Delete the remaining data buffers.
delete[] lpWideBuffer;
delete[] cpContext->lpBuffer;
cpContext->lpBuffer = NULL;
}
// Close the request and connect handles for this context.
Cleanup(cpContext);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"DATA_AVAILABLE Number of bytes available : %d. Reading next block of data", cpContext->dwSize);
// Otherwise, read the next block of data.
if (ReadData(cpContext) == FALSE)
{
swprintf(szBuffer, sizeof(szBuffer), L"DATA_AVAILABLE Number of bytes available : %d. ReadData returning FALSE", cpContext->dwSize);
Cleanup(cpContext);
}
}
break;
case WINHTTP_CALLBACK_STATUS_HANDLE_CREATED:
//An HINTERNET handle has been created. The lpvStatusInformation parameter contains a pointer to the HINTERNET handle.
if (lpvStatusInformation)
{
swprintf(szBuffer, sizeof(szBuffer), L"HANDLE_CREATED : %X", (unsigned int)lpvStatusInformation);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"HANDLE_CREATED (%d)", dwStatusInformationLength);
}
break;
case WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING:
//This handle value has been terminated. The lpvStatusInformation parameter contains a pointer to the HINTERNET handle. There will be no more callbacks for this handle.
if (lpvStatusInformation)
{
swprintf(szBuffer, sizeof(szBuffer), L"HANDLE_CLOSING : %X", (unsigned int)lpvStatusInformation);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"HANDLE_CLOSING (%d)", dwStatusInformationLength);
}
break;
case WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE:
//The response header has been received and is available with WinHttpQueryHeaders. The lpvStatusInformation parameter is NULL.
swprintf(szBuffer, sizeof(szBuffer), L"HEADERS_AVAILABLE (%d)", dwStatusInformationLength);
Header(cpContext);
// Initialize the buffer sizes.
cpContext->dwSize = 0;
cpContext->dwTotalSize = 0;
// Begin downloading the resource.
if (QueryData(cpContext) == FALSE)
{
Cleanup(cpContext);
}
break;
case WINHTTP_CALLBACK_STATUS_INTERMEDIATE_RESPONSE:
//Received an intermediate (100 level) status code message from the server.
//The lpvStatusInformation parameter contains a pointer to a DWORD that indicates the status code.
if (lpvStatusInformation)
{
swprintf(szBuffer, sizeof(szBuffer), L"INTERMEDIATE_RESPONSE Status code : %d", *(DWORD*)lpvStatusInformation);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"INTERMEDIATE_RESPONSE (%d)", dwStatusInformationLength);
}
break;
case WINHTTP_CALLBACK_STATUS_NAME_RESOLVED:
//Successfully found the IP address of the server. The lpvStatusInformation parameter contains a pointer to a LPWSTR that indicates the name that was resolved.
if (lpvStatusInformation)
{
swprintf(szBuffer, sizeof(szBuffer), L"NAME_RESOLVED : %s", (WCHAR *)lpvStatusInformation);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"NAME_RESOLVED (%d)", dwStatusInformationLength);
}
break;
case WINHTTP_CALLBACK_STATUS_READ_COMPLETE:
//Data was successfully read from the server. The lpvStatusInformation parameter contains a pointer to the buffer specified in the call to WinHttpReadData.
//The dwStatusInformationLength parameter contains the number of bytes read.
//When used by WinHttpWebSocketReceive, the lpvStatusInformation parameter contains a pointer to a WINHTTP_WEB_SOCKET_STATUS structure,
// and the dwStatusInformationLength parameter indicates the size of lpvStatusInformation.
swprintf(szBuffer, sizeof(szBuffer), L"READ_COMPLETE Number of bytes read : %d", dwStatusInformationLength);
// Copy the data and delete the buffers.
if (dwStatusInformationLength != 0)
{
TransferAndDeleteBuffers(cpContext, (LPSTR)lpvStatusInformation, dwStatusInformationLength);
// Check for more data.
if (QueryData(cpContext) == FALSE)
{
Cleanup(cpContext);
}
}
break;
case WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE:
//Waiting for the server to respond to a request. The lpvStatusInformation parameter is NULL.
swprintf(szBuffer, sizeof(szBuffer), L"RECEIVING_RESPONSE (%d)", dwStatusInformationLength);
break;
case WINHTTP_CALLBACK_STATUS_REDIRECT:
//An HTTP request is about to automatically redirect the request. The lpvStatusInformation parameter contains a pointer to an LPWSTR indicating the new URL.
//At this point, the application can read any data returned by the server with the redirect response and can query the response headers. It can also cancel the operation by closing the handle
if (lpvStatusInformation)
{
swprintf(szBuffer, sizeof(szBuffer), L"REDIRECT to %s", (WCHAR *)lpvStatusInformation);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"REDIRECT (%d)", dwStatusInformationLength);
}
break;
case WINHTTP_CALLBACK_STATUS_REQUEST_ERROR:
//An error occurred while sending an HTTP request.
//The lpvStatusInformation parameter contains a pointer to a WINHTTP_ASYNC_RESULT structure. Its dwResult member indicates the ID of the called function and dwError indicates the return value.
pAR = (WINHTTP_ASYNC_RESULT *)lpvStatusInformation;
swprintf(szBuffer, sizeof(szBuffer), L"REQUEST_ERROR - error %d, result %s", pAR->dwError, GetApiErrorString(pAR->dwResult));
//Error ERROR_INTERNET_NAME_NOT_RESOLVED
if (pAR->dwError == 0x12027)
{
//proxy name not resolved or host name not resolved
}
Cleanup(cpContext);
break;
case WINHTTP_CALLBACK_STATUS_REQUEST_SENT:
//Successfully sent the information request to the server.
//The lpvStatusInformation parameter contains a pointer to a DWORD indicating the number of bytes sent.
if (lpvStatusInformation)
{
swprintf(szBuffer, sizeof(szBuffer), L"REQUEST_SENT Number of bytes sent : %d", *(DWORD*)lpvStatusInformation);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"REQUEST_SENT (%d)", dwStatusInformationLength);
}
break;
case WINHTTP_CALLBACK_STATUS_RESOLVING_NAME:
//Looking up the IP address of a server name. The lpvStatusInformation parameter contains a pointer to the server name being resolved.
if (lpvStatusInformation)
{
swprintf(szBuffer, sizeof(szBuffer), L"RESOLVING_NAME %s", (WCHAR*)lpvStatusInformation);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"RESOLVING_NAME (%d)", dwStatusInformationLength);
}
break;
case WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED:
//Successfully received a response from the server.
//The lpvStatusInformation parameter contains a pointer to a DWORD indicating the number of bytes received.
if (lpvStatusInformation)
{
swprintf(szBuffer, sizeof(szBuffer), L"RESPONSE_RECEIVED. Number of bytes : %d", *(DWORD*)lpvStatusInformation);
}
else
{
swprintf(szBuffer, sizeof(szBuffer), L"RESPONSE_RECEIVED (%d)", dwStatusInformationLength);
}
break;
case WINHTTP_CALLBACK_STATUS_SECURE_FAILURE:
//One or more errors were encountered while retrieving a Secure Sockets Layer (SSL) certificate from the server.
/*If the dwInternetStatus parameter is WINHTTP_CALLBACK_STATUS_SECURE_FAILURE, this parameter can be a bitwise-OR combination of one or more of the following values:
WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED
Certification revocation checking has been enabled, but the revocation check failed to verify whether a certificate has been revoked.The server used to check for revocation might be unreachable.
WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT
SSL certificate is invalid.
WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED
SSL certificate was revoked.
WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA
The function is unfamiliar with the Certificate Authority that generated the server's certificate.
WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID
SSL certificate common name(host name field) is incorrect, for example, if you entered www.microsoft.com and the common name on the certificate says www.msn.com.
WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID
SSL certificate date that was received from the server is bad.The certificate is expired.
WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR
The application experienced an internal error loading the SSL libraries.
*/
if (lpvStatusInformation)
{
swprintf(szBuffer, sizeof(szBuffer), L"SECURE_FAILURE (%d).", *(DWORD*)lpvStatusInformation);
if (*(DWORD*)lpvStatusInformation & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED) //1
{
wcscat_s(szBuffer, L"Revocation check failed to verify whether a certificate has been revoked.");