How to programmatically send in Asp.Net a classic "multipart/form-data" form to a server?
Hi everyone ,
I've been searching for hours about a common Internet programming question and I'm certainly not the only one having the following problem :
With .Net (be it Delphi or VB), I need to POST a form to a .php server. If I pass the following parameter to the request :
objRequest.ContentType := 'application/x-www-form-urlencoded';
everything works fine. Except that if I need to transmit special characters from the form - like for example french or german accents - those laters are not transmitted in the format waited by the server (ISO-8859-1). So when you send text to the server via this form, you get unreadable or not corresponding character on the server side. Changing the content type like this on the *.aspx form source code doesn't change anything :
objRequest.ContentType := 'application/x-www-form-urlencoded; charset=ISO-8859-1';
No more result if you modify the web.config by adding the following line :
<globalization requestEncoding="ISO-8859-1" responseEncoding="ISO-8859-1" />
Another solution I tried was to HTML encrypt the parameter string posted back to server with the following .Net method :
FormString := HttpUtility.HtmlDecode(FormString);
No way. It makes new problem because of the "&" parameter separator... If you have special characters, it adds "&" in the string. For example the e-cute accent (é) is converted into é HTML code and the #233; is then considered as a parameter...
Some peolpe say that the content type "application/x-www-form-urlencoded" should not be used when you post a form. (there is also confusion between POST and GET method because usually you don't POST form be sending parameters into URL but this is another question). And I've remarked that actually, even if the server also accepts "application/x-www-form-urlencoded" form (certainly for compatibility reason), it is actually waiting for a "multipart/form-data" form. Posting form in "multipart/form-data" is also recommended by many people.
So, here is my question : how in Asp.Net to send programmatically to a server a classic "multipart/form-data" form containing characters of type ISO-8859-1 ? What must be the syntax of the string containing the content of the form and the request parameters ? With "application/x-www-form-urlencoded" form, the form parameter string is simple a line such as "parameter1=abc¶meter2=def". It doesn't work with "multipart/form-data" that encapsulate parameters between bounadaries. I found the following syntax exampe at
http://www.faqs.org/rfcs/rfc2388.html but I don't succeed in applying it to my application :
--AaB03x
content-disposition: form-data; name="parameter1"
content-type: text/plain;charset=ISO-8859-1
content-transfer-encoding: quoted-printable
abc
--AaB03x--AaB03x
content-disposition: form-data; name="parameter2"
content-type: text/plain;charset=ISO-8859-1
content-transfer-encoding: quoted-printable
def
--AaB03x
There are plenty of information and examples about using the "multipart/form-data" form to upload file on a server but I couldn't find any about simply sending a classic form including text fields with special characters like accents. If one knows how to do that, it would be great if one could put a short example onto your site ! I think it could be very usefull for a lot of people !
Thanks a lot for your precious attention!
Laurent,
Switzerland