Search code examples
c#webclientgoogle-translatesystem.net.webexception

C# - WebClient - Remoteserver send an 503 error


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=


Solution

  • 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://