Using HttpWebRequest to send POST data

S

supster

Hello, I am trying to use HttpWebRequest to simulate sending some POST
data from a form to a PHP script.

I have accomplished this using:

HttpWebRequest req = (HttpWebRequest)
WebRequest.Create("http://mysite.com/index.php");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string postData = "var=value1&var2=value2"
req.ContentLength = postData.Length;

StreamWriter stOut = new
StreamWriter(req.GetRequestStream(),
System.Text.Encoding.ASCII);
stOut.Write(postData);
stOut.Close();



I have now ran into a problem when I am trying to upload a file. The
PHP script expects a file uploaded from a form with the:
<input type="file">
object.

I have tried many things and can not figure out how to do this from
C#. Any help is greatly appreciated!
 
J

Joerg Jooss

supster said:
Hello, I am trying to use HttpWebRequest to simulate sending some POST
data from a form to a PHP script.

I have accomplished this using:

HttpWebRequest req = (HttpWebRequest)
WebRequest.Create("http://mysite.com/index.php");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string postData = "var=value1&var2=value2"
req.ContentLength = postData.Length;

StreamWriter stOut = new
StreamWriter(req.GetRequestStream(),
System.Text.Encoding.ASCII);
stOut.Write(postData);
stOut.Close();



I have now ran into a problem when I am trying to upload a file. The
PHP script expects a file uploaded from a form with the:
<input type="file">
object.

I have tried many things and can not figure out how to do this from
C#. Any help is greatly appreciated!

A file upload uses a different MIME type (multipart/form-data) and has
a more complicated structure than a simple form post; see
http://www.faqs.org/rfcs/rfc2388.html for details.

System.Net.WebClient.UploadFile() allows you to send such requests, but
it is broken in .NET 1.0 and .NET 1.1 (it is fixed in .NET 2.0). You'll
have to use HttpWebRequest instead to construct your
multipart/form-data request manually.

Cheers,
 
S

supster

Yes, you are correct, thanks a lot for the help Joerg.

I am able to do what you said with the following:


HttpWebRequest req = (HttpWebRequest)
WebRequest.Create("http://www.mysite.com/index.php");

string boundary =
Guid.NewGuid().ToString().Replace("-",
"");
req.ContentType = "multipart/form-data; boundary=" +
boundary;
req.Method = "POST";

MemoryStream postData = new MemoryStream();
string newLine = "\r\n";
StreamWriter sw = new StreamWriter(postData);
sw.Write("--" + boundary + newLine);
sw.Write("Content-Disposition: form-data;
name=\"{0}\";
filename=\"{1}\"{2}",
"upload", "test.jpg", newLine);
sw.Write("Content-Type: image/pjpeg " + newLine +
newLine);
sw.Flush();

postData.Write(contents, 0, contents.Length);
sw.Write(newLine);
sw.Write("--{0}--{1}", boundary,
newLine);
sw.Flush();

req.ContentLength = postData.Length;
using (Stream s = req.GetRequestStream())
postData.WriteTo(s);
postData.Close();


contents being a byte array of the file.

And this is successful. However, I need to be able to send that, as
well as normal POST data as my previous code example was doing.
However, I cannot figure out how to combine the two.

Any help?
 
S

supster

After taking a second look of that link you posted, I finaly figured
out how to do it!!

Basically you use the same method used to write the file to the
stream:

sw.Write("--" + boundary + newLine);
sw.Write("Content-Disposition: form-data;
name=\"FIELDNAME\"" + newLine);
sw.Write("content-type:
text/plain;charset=windows-1250" + newLine);
sw.Write("content-transfer-encoding:
quoted-printable" + newLine + newLine);
sw.Write("VALUE" + newLine);
sw.Write("--{0}--{1}", boundary,
newLine);
sw.Flush();


I am extremely happy now, I have been working on this all day long :)

Ok, now is there anyway to feed the req.GetResponse directly into
Internet Explorer? The only way I can think of is saving it as a file
on the local disk and then opening that in Internet Explorer. Does
.NET have a built in HTML Parser/Engine that I could use to parse the
data myself in my own built in "web browser" in the app?

Again, thanks so much!
 
J

Joerg Jooss

supster said:
After taking a second look of that link you posted, I finaly figured
out how to do it!!

Cool :-D

[...]
I am extremely happy now, I have been working on this all day long :)

Ok, now is there anyway to feed the req.GetResponse directly into
Internet Explorer? The only way I can think of is saving it as a file
on the local disk and then opening that in Internet Explorer.

The IE ActiveX control is probably able to read from a memory buffer,
but the file based approach will work for sure.
Does .NET have a built in HTML Parser/Engine that I could use to
parse > the data myself in my own built in "web browser" in the app?

Regarding HTML parsing, you can either try Chris Lovett's SgmlReader
(from www.GotDotNet.com) or the HtmlAgilityPack
(http://blogs.msdn.com/smourier/archive/2003/06/04/8265.aspx).

Cheers,
 

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