Encoding Question

Discussion in 'ASP .Net' started by =?Utf-8?B?U29kYQ==?=, Jan 2, 2005.

  1. 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 ..
    =?Utf-8?B?U29kYQ==?=, Jan 2, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?U29kYQ==?=

    Joerg Jooss Guest

    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
    Joerg Jooss, Jan 2, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hardy Wang

    Encoding.Default and Encoding.UTF8

    Hardy Wang, Jun 8, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    18,801
    Jon Skeet [C# MVP]
    Jun 9, 2004
  2. Replies:
    1
    Views:
    23,315
    Real Gagnon
    Oct 8, 2004
  3. Tony Vella

    page encoding question

    Tony Vella, Dec 19, 2005, in forum: HTML
    Replies:
    5
    Views:
    504
    Alan J. Flavell
    Dec 20, 2005
  4. Tony Vella

    page encoding question - thank you

    Tony Vella, Dec 20, 2005, in forum: HTML
    Replies:
    2
    Views:
    327
    Toby Inkster
    Dec 21, 2005
  5. Replies:
    2
    Views:
    354
Loading...

Share This Page