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

Encoding Question

P: n/a
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 ..
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


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

--
Joerg Jooss
www.joergjooss.de
ne**@joergjooss.de
Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.