1

场景:Windows Mobile C# Compact framework 2.0 或 3.5 Protobuf 对象

我需要将一个对象发送到一个 http url (Post)。之后,我将等待响应并收到对象的修改版本。关于如何连接到 http 流并传入序列化对象的任何输入?

4

1 回答 1

2

你的意思是沿着这些思路?

    private string SendData(string method, string directory, string data)
    {
        string page = string.Format("http://{0}/{1}", DeviceAddress, directory);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(page);
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;
        request.Method = method;

        // turn our request string into a byte stream
        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();

        // now send it
        requestStream.Write(postBytes, 0, postBytes.Length);
        requestStream.Close();

        HttpWebResponse response;

        response = (HttpWebResponse)request.GetResponse();

        return GetResponseData(response);
    }
于 2010-03-02T15:02:01.740 回答