473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to submit a POST with a WebRequest? (in C# to a PHP file)

there is a PHP file with which I try to communicate.
there is a simple HTML like that:
"<form action='feedback.php' method='POST'>blablabl</form>"

now I try to write some C# code to invoke the PHP from a desktop app, which
look like that:
====================
static HttpWebRequest CreateRequest(string url, string method,
IDictionary<string, stringparameters, Stream data)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = method;

foreach (string key in parameters.Keys)
request.Headers.Add(key, parameters[key]);

using (Stream reqStream = request.GetRequestStream())
{
//request.Headers.Add("Content-Length:", reqStream.Length.ToString());
CopyStream(data, reqStream);
}

return request;
}
====================
where URL is the URL of the PHP page.
unfortunately nothin is picked up by the PHP script.

did I miss something?
how do I submit multiple file btw?

Apr 24 '07 #1
3 17912
Try setting the ContentType property of the HttpWebRequest instance to
"application/x-www-form-urlencoded". Also, set the ContentLength to the
actual size of data being written to.

"Lloyd Dupont" wrote:
there is a PHP file with which I try to communicate.
there is a simple HTML like that:
"<form action='feedback.php' method='POST'>blablabl</form>"

now I try to write some C# code to invoke the PHP from a desktop app, which
look like that:
====================
static HttpWebRequest CreateRequest(string url, string method,
IDictionary<string, stringparameters, Stream data)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = method;

foreach (string key in parameters.Keys)
request.Headers.Add(key, parameters[key]);

using (Stream reqStream = request.GetRequestStream())
{
//request.Headers.Add("Content-Length:", reqStream.Length.ToString());
CopyStream(data, reqStream);
}

return request;
}
====================
where URL is the URL of the PHP page.
unfortunately nothin is picked up by the PHP script.

did I miss something?
how do I submit multiple file btw?

Apr 24 '07 #2
its unlikely that the php page is expecting the parameters as page
headers. its probably expecting a standard browser form post
(content-type=application/x-ww-form-urlencoded). th data is sent as name
value pairs (name=value) with "&" as a seperator. the name and value
should be urlencoded.

if you want to send a file, then the page is expecting a content-type:
multipart/mixed; boundary="myboundrystring". then standard mime headers
with content. (depends on type), delimted by boundary strings

google either mime type for more info.

-- bruce (sqlwork.com)

Lloyd Dupont wrote:
there is a PHP file with which I try to communicate.
there is a simple HTML like that:
"<form action='feedback.php' method='POST'>blablabl</form>"

now I try to write some C# code to invoke the PHP from a desktop app,
which look like that:
====================
static HttpWebRequest CreateRequest(string url, string method,
IDictionary<string, stringparameters, Stream data)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = method;

foreach (string key in parameters.Keys)
request.Headers.Add(key, parameters[key]);

using (Stream reqStream = request.GetRequestStream())
{
//request.Headers.Add("Content-Length:", reqStream.Length.ToString());
CopyStream(data, reqStream);
}

return request;
}
====================
where URL is the URL of the PHP page.
unfortunately nothin is picked up by the PHP script.

did I miss something?
how do I submit multiple file btw?
Apr 24 '07 #3
thanks bruce

--
Regards,
Lloyd Dupont
NovaMind Software
Mind Mapping at its best
www.nova-mind.com
"bruce barker" <no****@nospam.comwrote in message
news:OU**************@TK2MSFTNGP02.phx.gbl...
its unlikely that the php page is expecting the parameters as page
headers. its probably expecting a standard browser form post
(content-type=application/x-ww-form-urlencoded). th data is sent as name
value pairs (name=value) with "&" as a seperator. the name and value
should be urlencoded.

if you want to send a file, then the page is expecting a content-type:
multipart/mixed; boundary="myboundrystring". then standard mime headers
with content. (depends on type), delimted by boundary strings

google either mime type for more info.

-- bruce (sqlwork.com)

Lloyd Dupont wrote:
>there is a PHP file with which I try to communicate.
there is a simple HTML like that:
"<form action='feedback.php' method='POST'>blablabl</form>"

now I try to write some C# code to invoke the PHP from a desktop app,
which look like that:
====================
static HttpWebRequest CreateRequest(string url, string method,
IDictionary<string, stringparameters, Stream data)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = method;

foreach (string key in parameters.Keys)
request.Headers.Add(key, parameters[key]);

using (Stream reqStream = request.GetRequestStream())
{
//request.Headers.Add("Content-Length:", reqStream.Length.ToString());
CopyStream(data, reqStream);
}

return request;
}
====================
where URL is the URL of the PHP page.
unfortunately nothin is picked up by the PHP script.

did I miss something?
how do I submit multiple file btw?
Apr 26 '07 #4

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

Similar topics

0
by: ginette | last post by:
I really need help with a strange problem. i have a page having ONLY this cleaned and washed HTML code, no client scripting at all and "some" folks are unable to post any file till "some" are...
0
by: Marty | last post by:
I'm using a webrequest routine to download data from another website. Will a specific site (or system) directory need to have write permissions in order to facilitate the download of this data to...
1
by: Adnan Selimovic | last post by:
Hi folks! I tried to post a web form using the WebClient class and HttpWebRequest/HttpWebResponse. It didn't work for the ASPX web pages. I always got an unposted version - like if I had just made...
0
by: WIWA | last post by:
Hi, I want to login to a password protected website and fetch the content of the page behind. I have based my code on http://weblogs.asp.net/jdennany/archive/2005/04/23/403971.aspx. When I use...
3
by: Paul Phillips | last post by:
I have been struggling with this for a couple of weeks. I have a digital certificate installed in my certificate collection store on my local computer. The site I am trying to post to also has...
0
by: Paul Phillips | last post by:
I have been struggling with this for a couple of weeks. I have a digital certificate installed in my certificate collection store on my local computer. The site I am trying to post to also has...
0
by: pal | last post by:
MIME-Version: 1.0 Content-Type: multipart/related; boundary="ERCOT-XML-DATA"; Type="application/X-ERCOT-MARKETS" --ERCOT-XML-DATA Content-Type: application/xml <?xml version="1.0"...
4
by: Natalia | last post by:
Hello, I need to provide the ability to post file and some form elements via our website (asp.net) to the third party website (asp page). On http://aspalliance.com/236#Page4 - I found great...
8
by: Dave Siegel | last post by:
For some reason the submit POST and php on one form doesn't work for me. Here's my form code (it's dutch) <form method="POST"> <select name="locatie" size="1">...
0
by: Dimetrius | last post by:
Suppose we have a form at server, say, http://www.b.com/ (let's call it server Â): <form action="/" method="post" enctype="multipart/form-data"> <input name=file type=file size=20> <input...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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...
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,...

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.