Develop Windows 8-determine the network connection status

Source: Internet
Author: User

If the current network is not connected, an exception is thrown when you directly use postasync in httpclient to initiate a data request.

Therefore, the network status should be determined before network data interaction. If the Network is available, perform the network data request operation.

In Windows 8 API, you can use the networkconnectivitylevel interface to obtain the network status.

The Code is as follows:

/// <Summary> /// provides network-related data settings or retrieval functions // </Summary> public class networkhelper {public static bool isconnectedtointernet () {bool isconnected = false; connectionprofile CP = networkinformation. getinternetconnectionprofile (); If (CP! = NULL) {networkconnectivitylevel Cl = CP. getnetworkconnectivitylevel (); isconnected = (CL = networkconnectivitylevel. internetaccess);} return isconnected ;}}

This can be implemented as follows:

Httpclient = new httpclient (); If (networkhelper. isconnectedtointernet () {httpresponsemessage response = await httpclient. postasync (New uri (serveruri), new stringcontent (postdata); If (response. statuscode = httpstatuscode. OK) {// todo: processing the returned result }//...}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.