HTTP Post Method

B

bwilde

Hello,

I'm trying to write a C# App that posts a set of data to a server, and
I'm not having any luck figuring out how to get all of the parameters
submitted to the server correctly. I have an example of what the POST
should look like if it was coming from HTML, but I'm trying to convert
this to C# (I imagine that it will involve a WebRequest). Can
somebody please help me conver the following to C#? Thanks!!!

<FORM action="http://www.gpsvisualizer.com/map" method="POST">

<INPUT type="hidden" name="format" value="svg">
<INPUT type="hidden" name="bg_map" value="usgs_aerial">
<INPUT type="hidden" name="trk_colorize" value="altitude">
<INPUT type="hidden" name="units" value="us">
<INPUT type="hidden" name="legend_placement" value="topleft">

<INPUT type="hidden" name="special" value="test">
<INPUT type="hidden" name="filename" value="mydata.csv">
<INPUT type="hidden" name="data" value="
type,latitude,longitude,alt,name,desc
W,36.977000,-122.027000,7,SC,Santa Cruz
W,36.973000,-121.951000,3,Cap,Capitola
W,36.988000,-121.957000,36,Soq,Soquel
W,36.910000,-121.756000,9,Wat,Watsonville
T,36.972163,-122.035610,26,Track 1
T,36.974887,-122.033877,26
T,36.976557,-122.032195,29
T,36.976568,-122.032192,27
T,36.980547,-122.032227,22
T,36.984097,-122.030252,12
T,36.984108,-122.030257,12
T,36.985538,-122.025823,19
T,36.986478,-122.023475,22
T,36.988383,-122.021993,26
T,36.989220,-122.019613,29
T,36.988532,-122.011140,30
T,36.988223,-122.001135,37
T,36.987797,-121.991105,30
T,36.985890,-121.981562,40
T,36.983555,-121.972158,39
T,36.982738,-121.962395,33
T,36.983602,-121.952947,29
">

<INPUT type="submit" name="submitted" value="Draw the map">

</FORM>
 
N

Nicholas Paldino [.NET/C# MVP]

You will want to get a tool that will allow you to intercept the
requests sent to a server, something like Fiddler, from MS:

http://msdn2.microsoft.com/en-us/library/bb250446.aspx

This will allow you to see the content that is sent to the server, which
is what you will write to the stream returned by GetRequestStream on the
HttpWebRequest.

Also, you can see the HTTP 1.1 specification on how these values are
encoded according the the input elements in the form.
 
B

Ben Schwehn

Hello,

I'm trying to write a C# App that posts a set of data to a server, and
I'm not having any luck figuring out how to get all of the parameters
submitted to the server correctly. I have an example of what the POST
should look like if it was coming from HTML, but I'm trying to convert
this to C# (I imagine that it will involve a WebRequest). Can somebody
please help me conver the following to C#? Thanks!!!

Hello bwilde,

this snippet should get you started:
===
StringBuilder formRequest = new StringBuilder();

//foreach form
string encodedFormName = HttpUtility.UrlEncode(formName);
string encodedFormValue = HttpUtility.UrlEncode(formValue);
formRequest.Append(encodedFormName);
formRequest.Append("=");
formRequest.Append(encodedFormValue);
formRequest.Append("&");
//endforeach
byte[] postBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes
(formRequest.ToString());
targetRequest.ContentLength = postBuffer.Length;
targetRequest.ContentType = "application/x-www-form-urlencoded";
Stream requestStream = targetRequest.GetRequestStream();
requestStream.Write(postBuffer, 0, postBuffer.Length);
requestStream.Close();
===

hth
Ben
 

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