views:

73

answers:

1

Scenario: Windows Mobile C# Compact framework 2.0 or 3.5 Protobuf object

I need to send an object to a http url (Post). Afterward I will wait for a response and receive a modified version of the object back. Any input on how to connect to a http stream and passing in a serialized object?

+1  A: 

You mean something along these lines?

    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);
    }
ctacke
Thank you, this is a great start. My problem now occurs at:"postBytes = Encoding.UTF8.GetBytes(data);" and further at Stream requestStream = request.GetRequestStream(); // now send it requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close();Your object here,"data", is a string. I try to send an object of (my own written) class EventInfo. Could you comment on how you would proceed to Serialize and send that one instead of the string ?
Dipper
I have no idea how to serialize your object - only you would protobuf I believe has a serialization mechanism, bit essentailly you need to eitehr get it to XML and pass it in here (that's how I'm using it) or turn it into a byte array and then skip the Encoding.GetBytes() step.
ctacke
ok. thanks. I see my problem now is over to more to do with ow to serialize the object to the stream
Dipper