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

L

Lloyd Dupont

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, string> parameters, 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?
 
G

Guest

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.
 
B

bruce barker

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)
 
L

Lloyd Dupont

thanks bruce

--
Regards,
Lloyd Dupont
NovaMind Software
Mind Mapping at its best
www.nova-mind.com
bruce barker said:
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 said:
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, string> parameters, 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?
 

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