繁体   English   中英

HttpWebRequest无法使用Compact Framework

[英]HttpWebRequest Fails Compact Framework

尝试在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 Stream,直到我得到一个简单的请求为止

如果没有try catch我会收到“无法连接到远程服务器”的提示并崩溃。

原来这段代码很好

通过USB插入计算机后,设备将失去连接。
关闭Windows Mobile设备中心后,它工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM