中欧语言的UNICODE编码

时间:2012-11-03 11:16:12

标签: c# asp.net encoding

不承认不知道该做什么?

var url = "http://translate.google.ru/translate_a/t?client=x&text=" + text + "&hl=en&sl=en&tl=ru";

new System.Net.WebClient().DownloadFile(url, "filePath");

StreamReader streamReader = new StreamReader("filePath", Encoding.UTF8);

string trn = streamReader.ReadToEnd();
streamReader.Close();
return trn;


Label1.Text = tr.GoogleTranslate("testers");

结果:

1 个答案:

答案 0 :(得分:1)

在这里您可以使用WebRequest

阅读该内容
HttpWebResponse response = null;
StreamReader reader = null;
Stream dataStream = null;
StringBuilder sbReadBuffer = null;
int bufSizeMin = 8192;
int bufSizeMax = 65536;

try
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(TheWebPageToRead);

    if (req != null)
    {
        req.Method = "GET";
        response = (HttpWebResponse)req.GetResponse();

        if (response != null)
        {
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();

            // Open the stream using a StreamReader for easy access.
            reader = new StreamReader(dataStream, true);

            // get the length of the content returned by the request
            int length = (int)response.ContentLength;
            int bufSize = bufSizeMin;

            if (length > bufSize)
                bufSize = length > bufSizeMax ? bufSizeMax : length;

            // allocate buffer and StringBuilder for reading response
            byte[] buf = new byte[bufSize];
            sbReadBuffer = new StringBuilder(bufSize);

            // read the whole response
            while ((length = dataStream.Read(buf, 0, buf.Length)) != 0){
                sbReadBuffer.Append(Encoding.UTF8.GetString(buf, 0, length));
            }
        }
    }
}
finally
{
    if (response != null)
        response.Close();

    if (reader != null)
        reader.Close();

    if (dataStream != null)
        dataStream.Close();
}

TheWebPageToRead是您的网址 sbReadBuffer保留了返回并要求"testers"我得到:

{"sentences":[{"trans":"Тестеры","orig":"testers","translit":"Testery","src_translit":""}],"src":"en","server_time":11}

要求"aristos",我得到:{"sentences":[{"trans":"аристократов","orig":"aristos","translit":"aristokratov","src_translit":""}],"src":"en","server_time":5}