In windowmobile development, if the network is not connected successfully or the server address is incorrect, the software will crash when the program calls the WebService method.
There are the following solutions:
1. Download the server-specific static HTML to see if the download is successful. If the download is successful, the network is smooth.
1 // test whether the network connection is normal 2 Private bool testnet () 3 {4 try 5 {6 httpwebrequest request = (httpwebrequest) webrequest. create ("http: //" + textbox1.text + @ "/webtest.htm"); // a static html file address above the Server 7 Request. timeout = 10000; // connection timeout 8 webresponse response = request. getresponse (); 9 stream = response. getresponsestream (); 10 encoding = encoding. getencoding ("gb2312"); 11 streamreader reader = new strea Mreader (stream, encoding); 12 string result = reader. readtoend (); 13 reader. close (); 14 if (result. contains ("eluxianfeng") // static file content 15 {16 MessageBox. show ("server connection successful! "); 17 return true; 18} 19 else20 {21 MessageBox. Show (" server connection failed! Check whether the server address and port are correct! "); 22 return false; 23} 24} 25 catch (exception E1) 26 {27 statictools. wirtelog ("Test on server address:" + e1.message + e1.stacktrace); 28 MessageBox. show ("the server did not respond! Check whether the server address is correct or whether the network is connected! "); 29 cursor. Current = cursors. Default; 30 return false; 31} 32}
2. Directly determine whether WiFi is enabled (because the program is used for LAN ).
Using a namespace:
1 using Microsoft.WindowsMobile.Status;
// Test the connection private void button3_click (Object sender, eventargs e) {bool islineon = systemstate. wifistateconnected; // If (! Islineon) // if the network is not smooth {MessageBox. Show ("the network is not connected, check whether the network is smooth! "); Return;} cursor. current = cursors. waitcursor; // set the cursor to the waiting state testnet (); // download the specified static file cursor. current = cursors. default; // restore the initial state of the cursor}