Send (stream) a custom object to a http url

D

Dipper

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?
 
P

Peter Foot [MVP]

For that you can use the System.Net.HttpWebRequest class in System.dll. You
create a new HttpWebRequest using WebRequest.Create with the target Url,
then call GetRequestStream to get a stream you can write your object data
to, then call GetResponse to submit and receive a HttpWebResponse back. This
has a GetResponseStream which allows you to read the returned data. See the
examples here for using GetRequestStream:-
http://msdn.microsoft.com/en-us/library/d4cek6cc.aspx

Peter

--
Peter Foot
Microsoft Device Application Development MVP
peterfoot.net | appamundi.com | inthehand.com
APPA Mundi Ltd - software solutions for a mobile world
In The Hand Ltd - .NET Components for Mobility
 
D

Dipper

Thank you for reference to good example. My only issue with it is
that they stream a string object to the httpstream.
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] byte1 = encoding.GetBytes( (postData);

What would the equivalent be if one would go through serializing a
custom made object with the XMlSerializer?

Regards
 
P

Peter Foot [MVP]

The XmlSerializer.Serialize method has an overload which takes a Stream
object. You pass the request stream here to write the xml to it.

Peter

--
Peter Foot
Microsoft Device Application Development MVP
peterfoot.net | appamundi.com | inthehand.com
APPA Mundi Ltd - software solutions for a mobile world
In The Hand Ltd - .NET Components for Mobility

Dipper said:
Thank you for reference to good example. My only issue with it is
that they stream a string object to the httpstream.
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] byte1 = encoding.GetBytes( (postData);

What would the equivalent be if one would go through serializing a
custom made object with the XMlSerializer?

Regards

For that you can use the System.Net.HttpWebRequest class in System.dll.
You
create a new HttpWebRequest using WebRequest.Create with the target Url,
then call GetRequestStream to get a stream you can write your object data
to, then call GetResponse to submit and receive a HttpWebResponse back.
This
has a GetResponseStream which allows you to read the returned data. See
the
examples here for using
GetRequestStream:-http://msdn.microsoft.com/en-us/library/d4cek6cc.aspx

Peter

--
Peter Foot
Microsoft Device Application Development MVP
peterfoot.net | appamundi.com | inthehand.com
APPA Mundi Ltd - software solutions for a mobile world
In The Hand Ltd - .NET Components for Mobility
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top