Partager via


Network Programming in the .NET Compact Framework

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

The .NET Compact Framework provides built-in support for XML Web services and provides the following protocol support and functionality:

  • HTTP-based protocols.

  • NTLM Authentication.

  • SOAP-encoded XML content. This support includes passing ADO.NET datasets.

  • Web Request and Web Response methods that can send HTTP SOAP messages and receive SOAP messages in response.

  • SOAP libraries and methods that can serialize and deserialize method calls and arbitrary objects into and from XML SOAP messages.

HTTP Requests

The following items pertain to sending and receiving HTTP requests:

  • When using the emulator, do not use localhost for the server name. Specify the computer name or IP address of your development computer hosting the Web service.

    The emulator, like a device, has its own IP address. Using localhost instructs the emulator to use itself to connect to the Web service, rather than Web service hosted by your development environment or another desktop computer.

    For example, instead of the following:

    https://localhost/myWebService/Service1.asmx

    Specify the following:

    https://ComputerName/myWebService/Service1.asmx

  • When making an HTTP request using HttpWebRequest, the device initiates a new network connection if one is not available. Therefore, making an HTTP request to determine only if a connection is available can cause the device to attempt to initiate a connection, for example a GPRS connection.

  • The .NET Compact Framework does not store proxy information in the GlobalProxySelection.Select property, but it does use this property for HTTP connections if you specify a value in your code.

  • To connect to the Internet, you might need to specify your local proxy settings. The following code shows setting the proxy for port 80:

    System.Net.GlobalProxySelection.Select = new WebProxy("https://myproxy:80");
    
  • If you set AllowWriteStreamBuffering to false, the data will not be buffered in memory and will not support any authentication requests or redirections by the Web server.

  • To ensure successful operations, specify absolute path information.

    Note the following Windows CE behavior in resolving relative file specifications:

    file://myfile is resolved as \\myfile.

    file:///myfile is resolved as \myfile in the root directory.

  • It is a known issue that the .NET Framework Dns.GetHostName method throws an exception when there are more than 50 network protocols installed on the current machine.

    To work around this issue, uninstall the network protocols that are not actually needed. One way to do this is to use Windows Device Manager to remove unused network adapters. Another way is to uninstall applications that have installed protocols.

Secure Mobile Communications

There are two principal means for secure mobile communications:

  • HTTP Authentication

    The .NET Compact Framework supports Basic and Digest authentication. These authentication mechanisms are simple, and their security and tradeoffs are fairly well known – one being that the Web service is limited to an HTTP binding.

    .NET Compact Framework version 2.0 supports servers running NTLM or Kerberos ("Windows Integrated Authentication"), which does not require any code changes to existing Basic or Digest authentication.

  • Custom Security Headers

    The .NET Compact Framework does not currently support Web Services Security (WS-Security) and Web Service Enhancements (WSE).

    In addition, whether you authenticate using HTTP or a custom header, you can use SSL to increase security. Basic Authentication passes the name and password in clear text, so it is not particularly secure unless run from within SSL. However, when used in conjunction with SSL, it is fairly secure, with the only significant issue being accidentally revealing credentials to the target server.

Note   The .NET Compact Framework does not support client-side authentication using X509 certificates.

Content Length Considerations

When sending an HTTP Web request of streamed content using the POST protocol, you must specify a content length. Assuming SendChunked is false and Method = POST, specify a value for ContentLength.

Unlike the full .NET Framework, the .NET Compact Framework does not prebuffer data to accommodate memory constraint considerations. To ensure buffering, set SendChunked to false.

A request stream with zero content length causes an ObjectDisposedException if it was not obtained and closed correctly. To handle zero-content-length requests, you must explicitly call the GetRequestStream method and then call the Close method on the returned stream without calling the Write method, as shown by the following code example.

private static void ZeroLengthRequest()
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
        requestUri + "?dummy=true");
    request.AllowWriteStreamBuffering = true;
    request.Credentials = CredentialCache.DefaultNetworkCredentials;
    request.Credentials = netCred;
    request.ConnectionGroupName = "mygroup";
    request.ContentLength = 0;
    request.KeepAlive = true;
    request.Method = "POST";
    request.ServicePoint.UseNagleAlgorithm = false;
    request.Timeout = System.Threading.Timeout.Infinite;
    request.UnsafeAuthenticatedConnectionSharing = true;
    Stream req = request.GetRequestStream();
    req.Close();
    using (request.GetResponse())
    {
        ...
    }
}
Private Shared Sub ZeroLengthRequest()
        Dim request As HttpWebRequest = _
           CType(WebRequest.Create(requestUri + "?dummy=true"), _
           HttpWebRequest)
        request.AllowWriteStreamBuffering = true
        request.Credentials = CredentialCache.DefaultNetworkCredentials
        request.Credentials = netCred
        request.ConnectionGroupName = "mygroup"
        request.ContentLength = 0
        request.KeepAlive = true
        request.Method = "POST"
        request.ServicePoint.UseNagleAlgorithm = false
        request.Timeout = System.Threading.Timeout.Infinite
        request.UnsafeAuthenticatedConnectionSharing = true
        Dim req As Stream = request.GetRequestStream
        req.Close
        request.GetResponse
End Sub

See Also

Other Resources

Networking and Connectivity in the .NET Compact Framework