By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,434 Members | 2,902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,434 IT Pros & Developers. It's quick & easy.

problems with WebClient

P: n/a
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)

Feb 18 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
--
Milosz
"Giggi" wrote:
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)

Feb 19 '07 #2

P: n/a
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

Feb 19 '07 #3

P: n/a
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" wrote:
>
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

Feb 19 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.