Encoding Question

G

Guest

I try to write a ASP.Net web application which will post data to other
websites

I'm use NameValueCollection class add(...) method and WebClient class
uploadvalue(...) method to post data to other website. It works fine if
the data is in English, however, when it contain characters in other
languages (such as Chinese big5), it will display some un-readable
characters.

In web.config file I set encoding to "big5"
<globalization fileEncoding="big5" requestEncoding="big5"
responseEncoding="big5" />

In aspx page html I set codePage to "950"
<%@ Page Language="vb" validateRequest="false" AutoEventWireup="false"
Codebehind="message_post.aspx.vb" Inherits="message_post.WebForm1"
codePage="950" %>

If anyone knows how to solve this problem, please let me know .. Thanks ..
 
J

Joerg Jooss

Soda said:
I try to write a ASP.Net web application which will post data to other
websites

I'm use NameValueCollection class add(...) method and WebClient class
uploadvalue(...) method to post data to other website. It works fine
if the data is in English, however, when it contain characters in
other languages (such as Chinese big5), it will display some
un-readable characters.

In web.config file I set encoding to "big5"
<globalization fileEncoding="big5" requestEncoding="big5"
responseEncoding="big5" />

In aspx page html I set codePage to "950"
<%@ Page Language="vb" validateRequest="false" AutoEventWireup="false"
Codebehind="message_post.aspx.vb" Inherits="message_post.WebForm1"
codePage="950" %>

If anyone knows how to solve this problem, please let me know ..

<globalization /> has no effect on WebClient. WebClient.UploadValues() in
..NET 1.1 always uses UTF-8 internally. If you want to use a different
encoding like Big5, you'll need to use WebRequest or WebClient.UploadData(),
and perform the encoding manually:

public void UploadValues(string url, NameValueCollection data, string
encoding) {
Encoding enc = Encoding.GetEncoding(encoding);
StringBuilder builder = new StringBuilder();
foreach (string name in data) {
string encodedName = HttpUtility.UrlEncode(name, enc);
string encodedValue = HttpUtility.UrlEncode(data[name], enc);
builder.Append(encodedName);
builder.Append('=');
builder.Append(encodedValue);
builder.Append('&');
}
builder.Remove(builder.Length - 1, 1);
byte[] bytes = Encoding.ASCII.GetBytes(builder.ToString());
WebClient client = new WebClient();
client.Headers["Content-Type"] = String.Format(
"application/x-www-form-urlencoded; charset={0}",
encoding);
byte[] response = client.UploadData(url, bytes);
// Do something with the response...
}

Cheers,
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top