@@ -179,12 +179,16 @@ void ShutdownActiveThreads()
179
179
}
180
180
}
181
181
182
- int main ()
182
+ struct SampleHttpCallAsyncContext
183
+ {
184
+ hc_call_handle_t call;
185
+ bool isJson;
186
+ std::string filePath;
187
+ };
188
+
189
+ void DoHttpCall (std::string url, std::string requestBody, bool isJson, std::string filePath)
183
190
{
184
191
std::string method = " GET" ;
185
-
186
- std::string url = " https://raw.githubusercontent.com/Microsoft/libHttpClient/master/Samples/Win32-Http/TestContent.json" ;
187
- std::string requestBody = " " ;// "{\"test\":\"value\"},{\"test2\":\"value\"},{\"test3\":\"value\"},{\"test4\":\"value\"},{\"test5\":\"value\"},{\"test6\":\"value\"},{\"test7\":\"value\"}";
188
192
bool retryAllowed = true ;
189
193
std::vector<std::vector<std::string>> headers;
190
194
std::vector< std::string > header;
@@ -194,18 +198,6 @@ int main()
194
198
header.push_back (" 1.0" );
195
199
headers.push_back (header);
196
200
197
- HCInitialize (nullptr );
198
-
199
- uint32_t sharedAsyncQueueId = 0 ;
200
- CreateSharedAsyncQueue (
201
- sharedAsyncQueueId,
202
- AsyncQueueDispatchMode::AsyncQueueDispatchMode_Manual,
203
- AsyncQueueDispatchMode::AsyncQueueDispatchMode_Manual,
204
- &g_queue);
205
- RegisterAsyncQueueCallbackSubmitted (g_queue, nullptr , HandleAsyncQueueCallback, &g_callbackToken);
206
-
207
- StartBackgroundThread ();
208
-
209
201
hc_call_handle_t call = nullptr ;
210
202
HCHttpCallCreate (&call);
211
203
HCHttpCallRequestSetUrl (call, method.c_str (), url.c_str ());
@@ -220,42 +212,58 @@ int main()
220
212
221
213
printf_s (" Calling %s %s\r\n " , method.c_str (), url.c_str ());
222
214
215
+ SampleHttpCallAsyncContext* hcContext = new SampleHttpCallAsyncContext{ call, isJson, filePath };
223
216
AsyncBlock* asyncBlock = new AsyncBlock;
224
217
ZeroMemory (asyncBlock, sizeof (AsyncBlock));
225
- asyncBlock->context = call ;
218
+ asyncBlock->context = hcContext ;
226
219
asyncBlock->queue = g_queue;
227
220
asyncBlock->callback = [](AsyncBlock* asyncBlock)
228
221
{
229
222
const char * str;
230
- HRESULT errCode = S_OK;
223
+ HRESULT networkErrorCode = S_OK;
231
224
uint32_t platErrCode = 0 ;
232
225
uint32_t statusCode = 0 ;
233
226
std::string responseString;
234
227
std::string errMessage;
235
228
236
- hc_call_handle_t call = static_cast <hc_call_handle_t >(asyncBlock->context );
237
- HCHttpCallResponseGetNetworkErrorCode (call, &errCode, &platErrCode);
229
+ SampleHttpCallAsyncContext* hcContext = static_cast <SampleHttpCallAsyncContext*>(asyncBlock->context );
230
+ hc_call_handle_t call = hcContext->call ;
231
+ bool isJson = hcContext->isJson ;
232
+ std::string filePath = hcContext->filePath ;
233
+
234
+ HRESULT hr = GetAsyncStatus (asyncBlock, false );
235
+ if (FAILED (hr))
236
+ {
237
+ // This should be a rare error case when the async task fails
238
+ printf_s (" Couldn't get HTTP call object 0x%0.8x\r\n " , hr);
239
+ HCHttpCallCloseHandle (call);
240
+ return ;
241
+ }
242
+
243
+ HCHttpCallResponseGetNetworkErrorCode (call, &networkErrorCode, &platErrCode);
238
244
HCHttpCallResponseGetStatusCode (call, &statusCode);
239
245
HCHttpCallResponseGetResponseString (call, &str);
240
246
if (str != nullptr ) responseString = str;
241
247
std::vector<std::vector<std::string>> headers = ExtractAllHeaders (call);
242
248
243
- // Uncomment to write binary file to disk
244
- // size_t bufferSize = 0;
245
- // HCHttpCallResponseGetResponseBodyBytesSize(call, &bufferSize);
246
- // uint8_t* buffer = new uint8_t[bufferSize];
247
- // size_t bufferUsed = 0;
248
- // HCHttpCallResponseGetResponseBodyBytes(call, bufferSize, buffer, &bufferUsed);
249
- // HANDLE hFile = CreateFile(L"c:\\test\\test.zip", GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
250
- // DWORD bufferWritten = 0;
251
- // WriteFile(hFile, buffer, (DWORD)bufferUsed, &bufferWritten, NULL);
252
- // CloseHandle(hFile);
253
- // delete[] buffer;
249
+ if (!isJson)
250
+ {
251
+ size_t bufferSize = 0 ;
252
+ HCHttpCallResponseGetResponseBodyBytesSize (call, &bufferSize);
253
+ uint8_t * buffer = new uint8_t [bufferSize];
254
+ size_t bufferUsed = 0 ;
255
+ HCHttpCallResponseGetResponseBodyBytes (call, bufferSize, buffer, &bufferUsed);
256
+ HANDLE hFile = CreateFileA (filePath.c_str (), GENERIC_WRITE, 0 , NULL , CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL );
257
+ DWORD bufferWritten = 0 ;
258
+ WriteFile (hFile, buffer, (DWORD)bufferUsed, &bufferWritten, NULL );
259
+ CloseHandle (hFile);
260
+ delete[] buffer;
261
+ }
254
262
255
263
HCHttpCallCloseHandle (call);
256
264
257
265
printf_s (" HTTP call done\r\n " );
258
- printf_s (" Network error code: %d \r\n " , errCode );
266
+ printf_s (" Network error code: 0x%0.8x \r\n " , networkErrorCode );
259
267
printf_s (" HTTP status code: %d\r\n " , statusCode);
260
268
261
269
int i = 0 ;
@@ -265,7 +273,7 @@ int main()
265
273
i++;
266
274
}
267
275
268
- if (responseString.length () > 0 )
276
+ if (isJson && responseString.length () > 0 )
269
277
{
270
278
// Returned string starts with a BOM strip it out.
271
279
uint8_t BOM[] = { 0xef , 0xbb , 0xbf , 0x0 };
@@ -293,6 +301,25 @@ int main()
293
301
HCHttpCallPerformAsync (call, asyncBlock);
294
302
295
303
WaitForSingleObject (g_exampleTaskDone.get (), INFINITE);
304
+ }
305
+
306
+ int main ()
307
+ {
308
+ HCInitialize (nullptr );
309
+
310
+ uint32_t sharedAsyncQueueId = 0 ;
311
+ CreateSharedAsyncQueue (sharedAsyncQueueId,
312
+ AsyncQueueDispatchMode::AsyncQueueDispatchMode_Manual, AsyncQueueDispatchMode::AsyncQueueDispatchMode_Manual,
313
+ &g_queue);
314
+ RegisterAsyncQueueCallbackSubmitted (g_queue, nullptr , HandleAsyncQueueCallback, &g_callbackToken);
315
+ HCTraceSetTraceToDebugger (true );
316
+ StartBackgroundThread ();
317
+
318
+ std::string url1 = " https://raw.githubusercontent.com/Microsoft/libHttpClient/master/Samples/Win32-Http/TestContent.json" ;
319
+ DoHttpCall (url1, " {\" test\" :\" value\" },{\" test2\" :\" value\" },{\" test3\" :\" value\" },{\" test4\" :\" value\" },{\" test5\" :\" value\" },{\" test6\" :\" value\" },{\" test7\" :\" value\" }" , true , " " );
320
+
321
+ std::string url2 = " https://github.com/Microsoft/libHttpClient/raw/master/Samples/XDK-Http/Assets/SplashScreen.png" ;
322
+ DoHttpCall (url2, " " , false , " SplashScreen.png" );
296
323
297
324
ShutdownActiveThreads ();
298
325
HCCleanup ();
0 commit comments