473,795 Members | 3,175 Online
Bytes | Software Development & Data Engineering Community
+ 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.Crea te("http://mysite.com/index.php");
req.Method = "POST";
req.ContentType = "applicatio n/x-www-form-urlencoded";
string postData = "var=value1&var 2=value2"
req.ContentLeng th = postData.Length ;

StreamWriter stOut = new
StreamWriter(re q.GetRequestStr eam(),
System.Text.Enc oding.ASCII);
stOut.Write(pos tData);
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 40920
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.Crea te("http://mysite.com/index.php");
req.Method = "POST";
req.ContentType = "applicatio n/x-www-form-urlencoded";
string postData = "var=value1&var 2=value2"
req.ContentLeng th = postData.Length ;

StreamWriter stOut = new
StreamWriter(re q.GetRequestStr eam(),
System.Text.Enc oding.ASCII);
stOut.Write(pos tData);
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.WebC lient.UploadFil e() 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.d e
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("Conte nt-Disposition: form-data;
name=\"FIELDNAM E\"" + newLine);
sw.Write("conte nt-type:
text/plain;charset=w indows-1250" + newLine);
sw.Write("conte nt-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.Crea te("http://www.mysite.com/index.php");

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

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

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

req.ContentLeng th = postData.Length ;
using (Stream s = req.GetRequestS tream())
postData.WriteT o(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.d e
Nov 16 '05 #5

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

Similar topics

5
5087
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 ] sugapablo@12jabber.com <--jabber IM ]
2
1477
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
6958
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 work together with cookies, and was hoping that someone here could clear it up for me! Here's the steps I need to accomplish: 1) Set two cookies containing information I already have 2) Request the login page URL, and save a third cookie that...
2
1755
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
2399
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. This is my download code which work well in returning a large byte array from the server: Function GetImageFromURL(ByVal url As String) As Byte() allDone.Reset() Try Dim wr As HttpWebRequest = DirectCast(WebRequest.Create(url),...
1
1849
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 HttpWebRequest POST method: .... & _ "&severity='SEV 2', 'SEV 5'" & _ .... & _
0
12798
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 problem. I'm using this code in a ASP.NET 2.0 application just in case that matters, maybe someone knows a better way to do this?
0
1532
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 send a request to remote application. --------------------------------------------------------------------------------------------------------------- string lcUrl = "http://localhost/check/WebSite3/Default.aspx"; HttpWebRequest loHttp...
0
1232
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 posting Styled data: Forbidden You don't have permission to access /enrol/update.php on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10436
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6780
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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 we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.