-1

不认识解码怎么办?

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");

结果:������</p>

4

1 回答 1

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}

于 2012-11-03T12:37:00.320 回答