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 }//...}