Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpWebRequest在Compact框架中失败_C#_.net_Compact Framework_Http Request - Fatal编程技术网

C# HttpWebRequest在Compact框架中失败

C# HttpWebRequest在Compact框架中失败,c#,.net,compact-framework,http-request,C#,.net,Compact Framework,Http Request,试图在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 = (HttpWebReques

试图在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,我会出现“无法连接到远程服务器”并崩溃。

结果显示此代码没有问题

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