Windows Ce Compact Framework HttpWebRequest

Windows Ce Compact Framework HttpWebRequest

我正在尝试使用 rest apicontroller 上写入一些数据。

但奇怪的事情正在发生。前 1、2、3 次写入都很好,但应用程序冻结并出现超时异常,我是不是没有关闭某些东西?

这是下面的code

        WebRequest client = (read) ? HttpWebRequest.Create(ReadUri) : HttpWebRequest.Create(WriteUri);
        client.Method = "POST";
        client.ContentType = "application/json";
        CredentialCache creds = new CredentialCache();
        creds.Add(new Uri(WriteUri), "Basic", new NetworkCredential(*******));
        client.Credentials = creds;
        client.PreAuthenticate = true;
        ((HttpWebRequest)client).AllowWriteStreamBuffering = true;
        client.Timeout = 10000;
        //Skipped all data formatting
        byte[] data = Encoding.UTF8.GetBytes(req);
        client.ContentLength = data.Length;
        Stream dataStream = null; 
        try
        {
            //HttpWebResponse response = (HttpWebResponse)client.GetResponse();
            dataStream = client.GetRequestStream();

            dataStream.Write(data, 0, data.Length);
            dataStream.Flush();
            dataStream.Close();
            dataStream.Dispose();
            dataStream = null;

        }
        catch (WebException we)
        {
            string message = we.Message;
        }
        finally
        {
            client = null;
        }

发送请求后,您应该处理响应并释放它。如果不这样做,在后台请求及其响应将保持与连接相关联,默认情况下 .NET 只有几个可用连接 WebRequests。

...
dataStream.Close();
dataStream.Dispose();
dataStream = null;
using (HttpWebResponse response = (HttpWebResponse)client.GetResponse())
{
}