views:

85

answers:

2

Hi, I have a WM 6.1 Prof. application that checks updates when user wishes to do so.

I want to check if there is any avaliable connection (GPRS or Wifi) before it attempts to connect to server.

I am also using openNETCF.NET dll here is what I have done but it doesn't work everytime,

also I am not sure which type of connection should I use and so.

Ok do you think is this good?

Thank you very much!

  private static HttpWebRequest ConnectWeb(string urlx)
  {

      try
      {
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(urlx));

          request.ContentType = @"application/octet-stream";
          request.Credentials = CredentialCache.DefaultCredentials;
          request.Timeout(6000);
          return request;
      }
      catch (Exception ex)
      {
          MessageBox.Show(Lang.CONNECTIONPROBLEM);
          return null;
      }
  }


    private bool downloadTest()
    {

        Stream stream;
        HttpWebResponse response;
        HttpWebRequest request = ConnectWeb(FileManager.url);
        if (request!=null)
        {
            try
            {

                using (response = (HttpWebResponse)request.GetResponse())
                {

                    using (stream = response.GetResponseStream())
                    {
                        byte[] data = ReadFully(stream, (int)response.ContentLength);
                        writeByteArrayToFile(data, "data.zip");                                           
                    }                  
                }

                response.Close();
                stream.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(Lang.CONNECTIONPROBLEM);
                return false;
            }

}

A: 

Don't disconnect your connection manager.

As soon as you use the Windows Mobile Connection Manager, a plug-in to the network stack (the Autobind Winsock Layered Service Provider) starts automatically binding your network connections to the network interface corresponding to the chosen destination. Basically, it forces the packets to go the right way. If you request a disconnection, it may not send them at all.

Instead you should call Connect before you try to connect to your update server, then RequestDisconnect once you're done with it. Use an Asynchronous connection and attach to the Connected event if you want it to work in the background.

To follow what IE does with choosing the correct destination - WiFi or GPRS - use ConnectionManager.MapUrl to determine the destination GUID, and pass that into Connect. The default mapping behaviour is:

  • If the server-name part of the URL has no dots, it's a Work address
  • If the server-name part appears in the list of Exceptions, it's a Work address
  • Otherwise, it's an Internet address

What it then does depends on how ActiveSync/Windows Mobile Device Center is set up, if the device is cradled, and what is selected under 'My network card connects to', for WiFi (Start > Settings > Connections tab > Network Cards or WiFi icon). If this is set to 'Work' and the mapping is 'The Internet' it will never use WiFi. If it's set to 'The Internet' it will use WiFi if that is associated and fall back to GPRS if not.

As I recall, .NET CF's HttpWebRequest will automatically make use of the Connection Manager, following IE's behaviour of mapping the destination, so you may not need the OpenNETCF class at all.

Mike Dimmick
Thanks for your long answer, I willnot going to use opennetCF, instead I edited the code as above, you can check if thats ok
jan
A: 

ok HttpWebRequest is enough for testing. tnx

jan