problems with WebClient

G

Giggi

Hi!
I need to pass some strings via an HTTP POST from page1 (in my app) to
a page on another server.
I stored my data in a NameValueCollection and then sent it using
WebClient.UploadValues.
Everything works fine but if one of the string contains a european
character (èéòàù...) it's not sent correctly and the char is received
as a '?'
I read at least 20 different posts on similiar problem but didn't find
a solution yet.
any help?

Giggi

This is the code:

Dim uriString As String = "http://aqua.altervista.org/
prova.php"
Dim myWebClient As New Net.WebClient()
Dim myNameValueCollection As New NameValueCollection()

Dim MYTEXT As String = TextBox1.Text
Dim USER As String = TextBox2.Text

Dim responseArray As Byte()
Dim answer As String

' Add necessary parameter/value pairs to the name/value
container.
myNameValueCollection.Add("TEXT", MYTEXT)
myNameValueCollection.Add("USER", USER)

'The Upload(String,NameValueCollection)' method implicitly
sets the HTTP POST as the request method.
myWebClient.Credentials =
Net.CredentialCache.DefaultCredentials

responseArray = myWebClient.UploadValues(uriString,
myNameValueCollection)

'Decode the response.
answer= Encoding.ASCII.GetString(responseArray)
 
G

Guest

Howdy Giggi,

It's happening because your PHP page uses 'ISO 8859-1' code page. Characters
that cannot be recognised are automatically translated to 0x63 which is
equivalent to '?' in ISO 8859-1. Because ASP.NET works in Unicode by default,
WebClient does not send encoding information in outgoing POST
application/x-www-form-urlencoded request (if you create an empty page in
ASP.NET and request it in your code instead of php page, all characters will
be properly decoded). Therefore 'prova.php' page is unable to decode incoming
requests properly. I reckon you would have to amend your php page to handle
and serve unicode request. In addtion to that, request encoding can be
specified by adding charset header before calling UploadValues:

webClient.Headers.Add("CharSet", "UTF-8"); // or
webClient.Headers.Add("Charset", "text/html; charset=UTF-8");

Remember if you change request encoding to ASCII it won't help you with
european characters because they are handled differently than ISO 8859-1.
After you sort out PHP page, also remember to use Unicode/UTF8 in vb.net code

answer = Encoding.Unicode.GetString(response)

Hope it helps
 
G

Giggi

It's happening because your PHP page uses 'ISO 8859-1' code page. Characters
that cannot be recognised are automatically translated to 0x63 which is
equivalent to '?' [CUT]
request encoding can be
specified by adding charset header before calling UploadValues:

webClient.Headers.Add("CharSet", "UTF-8"); // or
webClient.Headers.Add("Charset", "text/html; charset=UTF-8");

Hi Milosz!
Thank you very much for the answer,
I tried to add the charset header but it doesn't work. I also tried to
add:

myWebClient.Encoding = System.Text.Encoding.UTF8

The php page still receives ? instead of the european character.
But the real problem is that the PHP page is a page I created for
testing purpose in order to read what an external page receives from
my HTTP POST. The real external page is a classic .ASP page that allow
me to send an SMS.
Any other idea? Should I use httpwebrequest instead?

Giggi
 
G

Guest

Good morning Giggi,

As i suggested in previous reply the problem is your test PHP page. Whatever
encoding you set in webclient or webrequest classes instance, prova.php
always treats incoming request as 'ISO 8859-1' encoded. I created a test
classic ASP page, and everything is working like a charm. I haven’t used PHP
in my life so I have no idea what needs to be done to support Unicode
characters. Anyway, you said sms sending page is a classic ASP page, so
shouldn’t you create a test ASP page as a testing eniroment?

Hope this helps
--
Milosz


Giggi said:
It's happening because your PHP page uses 'ISO 8859-1' code page. Characters
that cannot be recognised are automatically translated to 0x63 which is
equivalent to '?' [CUT]
request encoding can be
specified by adding charset header before calling UploadValues:

webClient.Headers.Add("CharSet", "UTF-8"); // or
webClient.Headers.Add("Charset", "text/html; charset=UTF-8");

Hi Milosz!
Thank you very much for the answer,
I tried to add the charset header but it doesn't work. I also tried to
add:

myWebClient.Encoding = System.Text.Encoding.UTF8

The php page still receives ? instead of the european character.
But the real problem is that the PHP page is a page I created for
testing purpose in order to read what an external page receives from
my HTTP POST. The real external page is a classic .ASP page that allow
me to send an SMS.
Any other idea? Should I use httpwebrequest instead?

Giggi
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top