2

I wrote a little windows forms program that sends a string to google translate. The target framework is .Net 4.5.2

 public string TranslateText(string input, string languagePair)
            {
                try
                {
                    string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
                    WebClient webClient = new WebClient();
                    webClient.Encoding = System.Text.Encoding.UTF8;
                    string result = webClient.DownloadString(url);
                    result = result.Substring(result.IndexOf("<span title=\"") + "<span title=\"".Length);
                    result = result.Substring(result.IndexOf(">") + 1);
                    result = result.Substring(0, result.IndexOf("</span>"));
                    return result.Trim();
                }
                catch (WebException wex)
                {
                    var err = "";
                    using (var sr = new StreamReader(wex.Response.GetResponseStream()))
                        err= sr.ReadToEnd();
                    saveLog(err);
                    return "FEHLER";
                }
}

So my problem is, that on my machine the code works fine. But on the computer of my customer, there is always a System.Net.WebException. "The remote server returned an error: (503) Server Unavailable". Here is the complete error:

System.Net.WebException: The remote server returner an error: (503) Server Unavailable.
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadString(Uri address)
   at WindowsFormsApplication1.Form1.TranslateText(String input, String languagePair)

I don't know, what I should do. I've tried to deactivate anti virus software and the firewall. Also I tried to copy the url string to a browser, with some valid inputs (http://www.google.com/translate_t?hl=en&ie=UTF8&text=test&langpair=de|en)

Anything else what I can try? Thank you for your time and your answers! Best regards, Dominik

EDIT: Problem solved. First I changed the url to https://translate.google.com/?hl=en&;ie=UTF8&text={0}&langpair={1} and then I received captcha problems. I solved this by adding "&client=" at the end of the url: translate.google.com/?hl=en&;ie=UTF8&text={0}&langpair={1}&client=

Schlodi
  • 149
  • 1
  • 15
  • `http` is not accepted by Google. Change it to `https`: `string url = String.Format("https://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);` – NoName Jun 15 '16 at 09:41
  • @sakura I don't belive this is the issue as the above code works fine in LinqPad on my machine.. Or maybe it since I'll probably be getting a 301 redirect to https. Maybe that could be an issue – Jordan Coulam Jun 15 '16 at 09:48
  • your code is not the good way to go. you can use [this code](https://github.com/Airstriker/GoogleTranslator/blob/master/Translator.cs) which is better. Example of use: `https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=de&dt=t&q=my+name+is+Sakura` – NoName Jun 15 '16 at 10:02
  • Thanks for your help and thanks for the code. At the moment everything works fine with the answer of Jordan Coulam. But when I face new problems, I will definitely use your code. Thanks everyone! – Schlodi Jun 15 '16 at 10:54

2 Answers2

1

Trying changing your url to this https://translate.google.com/?hl=en&;ie=UTF8&text={0}&langpair={1} This will prevent 2 redirects. 1 to to this changed url and a seconds redirect to https://

Jordan Coulam
  • 359
  • 1
  • 13
  • 1
    Thanks man, that helped me. After this change I got an error with the captcha, but I fixed it by adding "&client=" at the end of the url: https://translate.google.com/?hl=en&;ie=UTF8&text={0}&langpair={1}&client= – Schlodi Jun 15 '16 at 10:49
0

Have you checked VPN connection or proxy settings on your client's computer?

Fka
  • 6,044
  • 5
  • 42
  • 60