364,033 Members | 4753 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Using HttpWebRequest to send POST data

supster
P: n/a
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!


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


Joerg Jooss
P: n/a
Joerg Jooss
supster wrote:
[color=blue]
> 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![/color]

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,
--
http://www.joergjooss.de
mailto:news-reply@joergjooss.de
Nov 16 '05 #2

supster
P: n/a
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!


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #3

supster
P: n/a
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?


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #4

Joerg Jooss
P: n/a
Joerg Jooss
supster wrote:
[color=blue]
> After taking a second look of that link you posted, I finaly figured
> out how to do it!!
>[/color]

Cool :-D

[...][color=blue]
> 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.[/color]

The IE ActiveX control is probably able to read from a memory buffer,
but the file based approach will work for sure.
[color=blue]
> Does .NET have a built in HTML Parser/Engine that I could use to[/color]
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/archi.../04/8265.aspx).

Cheers,
--
http://www.joergjooss.de
mailto:news-reply@joergjooss.de
Nov 16 '05 #5

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp c# postdata