次の方法で共有


Walkthrough: Connecting Using Tasks and XML HTTP Requests

This example shows how to use the IXMLHTTPRequest2 and IXMLHTTPRequest2Callback interfaces together with tasks to send HTTP GET and POST requests to a web service in a Windows Store app. By combining IXMLHTTPRequest2 together with tasks, you can write code that composes with other tasks. For example, you can use the download task as part of a chain of tasks. The download task can also respond when work is canceled.

Tip

You can also use the C++ REST SDK to perform HTTP requests from a Windows Store app using C++ app or from a desktop C++ app. For more info, see C++ REST SDK (Codename "Casablanca").

For more information about tasks, see Task Parallelism (Concurrency Runtime). For more information about how to use tasks in a Windows Store app, see Asynchronous programming in C++ and Creating Asynchronous Operations in C++ for Windows Store Apps.

This document first shows how to create HttpRequest and its supporting classes. It then shows how to use this class from a Windows Store app that uses C++ and XAML.

For a more complete example that uses the HttpReader class described in this document, see Developing Bing Maps Trip Optimizer, a Windows Store app in JavaScript and C++. For another example that uses IXMLHTTPRequest2 but does not use tasks, see Quickstart: Connecting using XML HTTP Request (IXMLHTTPRequest2).

Tip

IXMLHTTPRequest2 and IXMLHTTPRequest2Callback are the interfaces that we recommend for use in a Windows Store app. You can also adapt this example for use in a desktop app.

Defining the HttpRequest, HttpRequestBuffersCallback, and HttpRequestStringCallback Classes

When you use the IXMLHTTPRequest2 interface to create web requests over HTTP, you implement the IXMLHTTPRequest2Callback interface to receive the server response and react to other events. This example defines the HttpRequest class to create web requests, and the HttpRequestBuffersCallback and HttpRequestStringCallback classes to process responses. The HttpRequestBuffersCallback and HttpRequestStringCallback classes support the HttpRequest class; you work only with the HttpRequest class from application code.

The GetAsync, PostAsync methods of the HttpRequest class enable you to start HTTP GET and POST operations, respectively. These methods use the HttpRequestStringCallback class to read the server response as a string. The SendAsync and ReadAsync methods enable you to stream large content in chunks. These methods each return concurrency::task to represent the operation. The GetAsync and PostAsync methods produce task<std::wstring> value, where the wstring part represents the server’s response. The SendAsync and ReadAsync methods produce task<void> values; these tasks complete when the send and read operations complete.

Because the IXMLHTTPRequest2 interfaces act asynchronously, this example uses concurrency::task_completion_event to create a task that completes after the callback object completes or cancels the download operation. The HttpRequest class creates a task-based continuation from this task to set the final result. The HttpRequest class uses a task-based continuation to ensure that the continuation task runs even if the previous task produces an error or is canceled. For more information about task-based continuations, see Task Parallelism (Concurrency Runtime)

To support cancellation, the HttpRequest, HttpRequestBuffersCallback, and HttpRequestStringCallback classes use cancellation tokens. The HttpRequestBuffersCallback and HttpRequestStringCallback classes use the concurrency::cancellation_token::register_callback method to enable the task completion event to respond to cancellation. This cancellation callback aborts the download. For more info about cancellation, see Cancellation in the PPL.

To Define the HttpRequest Class

  1. Use the Visual C++ Blank App (XAML) template to create a blank XAML app project. This example names the project UsingIXMLHTTPRequest2.

  2. Add to the project a header file that is named HttpRequest.h and a source file that is named HttpRequest.cpp.

  3. In pch.h, add this code:

  4. In HttpRequest.h, add this code:

  5. In HttpRequest.cpp, add this code:

Using the HttpRequest Class in a Windows Store App

This section demonstrates how to use the HttpRequest class in a Windows Store app. The app provides an input box that defines a URL resource, and button commands that perform GET and POST operations, and a button command that cancels the current operation.

To Use the HttpRequest Class

  1. In MainPage.xaml, define the StackPanel element as follows.

  2. In MainPage.xaml.h, add this #include directive:

  3. In MainPage.xaml.h, add these private member variables to the MainPage class:

  4. In MainPage.xaml.h, declare the private method ProcessHttpRequest:

  5. In MainPage.xaml.cpp, add these using statements:

  6. In MainPage.xaml.cpp, implement the GetButton_Click, PostButton_Click, and CancelButton_Click methods of the MainPage class.

    Tip

    If your app does not require support for cancellation, pass concurrency::cancellation_token::none to the HttpRequest::GetAsync and HttpRequest::PostAsync methods.

  7. In MainPage.xaml.cpp, implement the MainPage::ProcessHttpRequest method.

  8. In the project properties, under Linker, Input, specify shcore.lib and msxml6.lib.

Here is the running app:

The running Windows Store app

Next Steps

Concurrency Runtime Walkthroughs

See Also

Reference

task Class (Concurrency Runtime)

task_completion_event Class

Concepts

Task Parallelism (Concurrency Runtime)

Cancellation in the PPL

Creating Asynchronous Operations in C++ for Windows Store Apps

Other Resources

Asynchronous programming in C++

Quickstart: Connecting using XML HTTP Request (IXMLHTTPRequest2)

IXMLHTTPRequest2

IXMLHTTPRequest2Callback