Condividi tramite


How to use IXMLHttpRequest2 with a network trigger (XAML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

This topic shows how to maintain an IXMLHTTPRequest2 network connection in a Windows Store app using ControlChannelTrigger when an app is in the background .

What you need to know

Technologies

Prerequisites

  • The following information applies to any connected or network-aware Windows Store app that depends on network connections using IXMLHTTPRequest2 to always be connected. The IXMLHTTPRequest2 interface provides access to HTTP servers and other web services. This topic applies to apps written in C++/XAML and apps using the .NET Framework 4.5 in C#, VB.NET, or managed C++ on Windows 8 and Windows Server 2012.

    This topic does not apply to apps written in JavaScript or a foreground app in JavaScript with an in-process C# or C++ binary. Background network connectivity using a network trigger with ControlChannelTrigger is not supported by a JavaScript app. For information on background tasks that apply to JavaScript apps, see Supporting your app with background tasks. For information on background network connectivity supported by a JavaScript app, see Staying connected in the background (HTML).

Instructions

Step 1: Using IXMLHTTPRequest2 with ControlChannelTrigger

Some special considerations apply when using IXMLHTTPRequest2 with ControlChannelTrigger. There are some transport-specific usage patterns and best practices that should be followed when using a IXMLHTTPRequest2 with ControlChannelTrigger. Using ControlChannelTrigger does not affect the way that requests to send or receive HTTP requests on the IXMLHTTPRequest2 are handled.

Usage patterns and best practices when using IXMLHTTPRequest2 with ControlChannelTrigger

  • An IXMLHTTPRequest2 object when used as the transport has a lifetime of only one request/response. When used with the ControlChannelTrigger object, it is convenient to create and set up the ControlChannelTrigger object once and then call the UsingTransport method repeatedly, each time associating a new IXMLHTTPRequest2 object. An app should delete the previous IXMLHTTPRequest2 object before supplying a new IXMLHTTPRequest2 object to ensure that the app does not exceed the allocated resource limits.
  • The app may need to call the SetProperty and SetRequestHeader methods to set up the HTTP transport before calling Send method.
  • An app may need to make need to an initial Send request to test and setup the transport properly before creating the transport to be used with ControlChannelTrigger. Once the app determines that the transport is properly setup, the IXMLHTTPRequest2 object can be configured as the transport object used with the ControlChannelTrigger. This process is designed prevent some scenarios from breaking the connection established over the transport. Using SSL with a certificate, an app may require a dialog to be displayed for PIN entry or if there are multiple certificates to choose from. Proxy authentication and server authentication may be required. If the proxy or server authentication expires, the connection may be closed. One way an app can deal with these authentication expiration issues is to set a timer. When an HTTP redirect is required, it is not guaranteed that the second connection can be established reliably. An initial test request will ensure that the app can use the most up-to-date redirected URL before using the IXMLHTTPRequest2 object as the transport with the ControlChannelTrigger object.

For more information on using IXMLHTTPRequest2 with ControlChannelTrigger, see the ControlChannelTrigger with IXMLHTTPRequest2 sample.

Step 2: Previous steps

For more information on how to create a lock screen app to receive background network notifications that use network triggers, see Quickstart: Create a lock screen app that uses background network triggers.

For more information on how to use network triggers to deliver notifications to a lock screen app, see How to use network triggers to deliver notifications to a lock screen app.

For more information on how to write a background task to receive background network notifications that use network triggers, see How to write a background task for a network trigger.

For more information on how to re-establish a network trigger and a transport connection, see How to re-establish a network trigger and transport connection.

Step 3: Further steps

For more information on guidelines and checklists for using network triggers, see Guidelines and checklist for using network triggers.

Other resources

Adding support for networking

Background Networking

Badge overview

Connecting to a web service

Lock screen overview

Staying connected in the background

Supporting your app with background tasks

Tile and tile notification overview

Toast notification overview

Transferring data in the background

Troubleshooting and debugging network connections

Reference

ControlChannelTrigger

IXMLHTTPRequest2

Windows.ApplicationModel.Background

Windows.Networking.BackgroundTransfer

Windows.Networking.PushNotifications

Windows.Networking.Sockets

Samples

Background task sample

ControlChannelTrigger with IXMLHTTPRequest2 sample

Lock screen apps sample

Push and periodic notifications client-side sample

Raw notifications sample