473,467 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using HttpWebRequest to send POST data

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
4 40894
supster wrote:
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,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 16 '05 #2
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
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
supster wrote:
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/archi.../04/8265.aspx).

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sugapablo | last post by:
I'd like to send POST data to one web page but bring up a different page in the browser. Is this possible? -- http://www.sugapablo.com <--music ] http://www.sugapablo.net <--personal ...
2
by: desalvo123 | last post by:
Is there a way I can manually create and send POST data in my code to a website without using html forms?
2
by: Luis Esteban Valencia Muñoz | last post by:
I'm working on a web scraping application that needs to log into a website before it can get the data to scrape. I've always been confused about how the HttpWebRequest and HttpWebResponse objects...
2
by: Tamas Nyilanszky | last post by:
hello, i'd like to be able to send post data to a webpage from within a vb .net application. how would you do it?
0
by: Fred Herring | last post by:
I currently use httpwebrequest to download large byte array files from my server. I am curious about how to change my httpwebrequest code to allow posing a large byte array to a virtual foldeer. ...
1
by: Terry Olsen | last post by:
Using the HttpWebRequest to post data for a query, the input tag that i'm mimicking is: <input type "hidden" name="severity" value="'SEV 2', 'SEV 5'"> I'm using this for making the query via...
0
by: barrybevel | last post by:
Hi, I'm trying to login to the www.vodafone.ie website using HttpWebRequest. It works fine with IE/Firefox and the .NET Web Control too, just not with my code. I think it's a redirect 302...
0
by: sanjaygupta11 | last post by:
I am using httpwebrequest and httpwebresponse objects for sending data to remote appication by post and receiving the response from there. I am using this code in my windows application which will...
0
by: ashokjbp | last post by:
I am using FCKeditor in a PHP form to send POST data to my Shared Server for updating a MySQL table. My update.php page is accepting plain text and the server gives the following message if I am...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.