0

I wrote a metro style application to call bing translator, but it didnt work. I tried to debug this program, but the UI does not respond. I can't figure if it is the URI I am using, or some other problem.

Here my code :

public string GetTranslatedText(string textToTranslate, string fromLang, string toLang)
{
    string translation;

    if (fromLang != toLang)
    {
        string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=" +
                    appID + "&text=" + textToTranslate + "&from=" + fromLang + "&to=" + toLang;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

        try
        {
            WebResponse response = request.GetResponse();
            Stream strm = response.GetResponseStream();
            StreamReader sr = new StreamReader(strm);
            translation = sr.ReadToEnd();

            response.Close();
            sr.Close();
        }
        catch (WebException)
        {
            MessageBox.Show("Ensure that you are connected to the internet.",
                        "Translator", MessageBoxButton.OK, MessageBoxImage.Stop);
            return (string.Empty);
        }
    }
    else
    {
        MessageBox.Show("Will not translate to the same language.", "Translator",
                    MessageBoxButton.OK, MessageBoxImage.Exclamation);
        return (string.Empty);
    }

    // Parse string into an XElement and get the XElement Value
    // which is returned as the translated text.
    return (XElement.Parse(translation).Value);
}
4

2 回答 2

2

自四月起,您还需要一个访问令牌

http://msdn.microsoft.com/en-us/library/hh454950.aspx

于 2012-09-10T15:11:01.317 回答
0

您需要发送请求到
https://api.microsofttranslator.com/v2/ajax.svc/TranslateArray2
而不是
http://api.microsofttranslator.com/v2/Http.svc/Translate

于 2015-11-04T07:56:14.217 回答