Home All Groups Group Topic Archive Search About

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

Author
24 Apr 2007 1:34 AM
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?

Author
24 Apr 2007 5:12 AM
Siva M
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.

Show quote
"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, 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?
>
>
Author
24 Apr 2007 3:41 PM
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)



Lloyd Dupont wrote:
Show quote
> 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?
>
Author
26 Apr 2007 1:53 AM
Lloyd Dupont
thanks bruce

--
Regards,
Lloyd Dupont
NovaMind Software
Mind Mapping at its best
www.nova-mind.com
Show quote
"bruce barker" <nospam@nospam.com> wrote in message
news:OUH1wdohHHA.4588@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, 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?
>>

AddThis Social Bookmark Button