Soda wrote:[color=blue]
> 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 ..[/color]
<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 news@joergjooss.de