HttpWebRequest 失败的紧凑框架

本文关键字:框架 失败 HttpWebRequest | 更新日期: 2023-09-27 18:35:07

尝试在 Windows Mobile 6.5 设备上发出 Http POST 请求

我可以在设备上的浏览器中查看网页,但 Http 请求失败

private string SendData(string directory, string data)
    {
        string page = string.Format("http://{0}/{1}", hostname, directory);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(page);
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;
        request.Method = "POST";
        request.ContentType = "text/json";
        byte[] postBytes;
        if (data != null)
        {
            postBytes = Encoding.UTF8.GetBytes(data);
        }
        else
        {
            postBytes = new byte[0];
        }
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postBytes.Length;
        /*
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(postBytes, 0, postBytes.Length);
        requestStream.Close();
        */
        HttpWebResponse response = null;
        try
        {
            response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
                Console.WriteLine("'r'nResponse Status Code is OK and StatusDescription is: {0}",
                                     response.StatusDescription);
        }
        catch (WebException e) {
            Debug.WriteLine("'r'nWebException Raised. The following error occured : "+e.Status);
        }
    }

我现在已经注释掉了 POST 流,直到我可以得到一个简单的请求工作

如果没有try catch,我会收到"无法连接到远程服务器"并崩溃。

HttpWebRequest 失败的紧凑框架

原来这段代码很好

当通过USB插入计算机时,设备会失去连接。
关闭Windows移动设备中心后,它工作正常。