Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,
I want to translate from English to Malay using Google API.
I have the following code:
C#
public static string TranslateText(string input, string languagePair, Encoding encoding)
    {
      string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
      string result = String.Empty;
      using (WebClient webClient = new WebClient())
      {
        webClient.Encoding = encoding;
        NetworkCredential netcredit = new NetworkCredential("MyNetworkUserID", "MyNetworkPassword", "corp");
        webClient.Credentials = netcredit;
        webClient.Proxy = new System.Net.WebProxy()
        {
            Credentials = new System.Net.NetworkCredential("MyNetworkUserID", "MyNetworkPassword", "corp")
        };
       // webClient.Proxy = null;//if I don't give webClient.Proxy-it returns Error "407-ProxyAuthentication Required!"
        result = webClient.DownloadString(url);
          // result = webClient.DownloadString("http://www.google.com/");//just to check with google itself,still the same error
      }
      Match m = Regex.Match(result, "(?<=<div id=result_box dir=\"ltr\">)(.*?)(?=</div>)");
      if (m.Success)
        result = m.Value;
else
result="Failure";
      return result;
    }

After running this:Getting the following error:Unable to connect Remote Server,the detail error msg is as below:
"No connection could be made because the target machine actively refused it 209.85.231.104:80"
Where did I go wrong?Please help me.
Posted
Updated 29-Nov-10 2:01am
v2

What happens if you don't set credentials on the webclient. It's very likely that you need the credentials for the proxy, but not for google translate. Another thing that caught my attention is that you didn't explicitely specify the proxy servers address.


Modification


Use this to create the web proxy:
WebProxy proxyObject = new WebProxy("http://nameOfYourProxyServer:80/");
proxyObject.Credentials =  new System.Net.NetworkCredential("MyNetworkUserID", "MyNetworkPassword", "corp");

You'll have to substitute the name of your proxy server in the construtor of WebProxy and I'm assuming here that port 80 is the proxy port. You'll have to ask your network administrator what name and port to use.

Cheers

Manfred
 
Share this answer
 
v3
Comments
avishekrc 30-Nov-10 1:21am    
1.What happens if you don't set credentials on the webclient?It is thowing the same error.
2.How to specify the proxy servers address here?Give me some Idea.
avishekrc 30-Nov-10 2:39am    
Hi,
I am getting the error:407:Proxy Authentication Required!
This is how my new function look like:
public static string TranslateText(string input, string languagePair, Encoding encoding)
{
string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
string result = String.Empty;
WebClient webClient = new WebClient();
webClient.Encoding = encoding;
NetworkCredential netcredit = new NetworkCredential("archowdhury", "Secure*45", "corp");
webClient.Credentials = netcredit;
WebProxy proxyObject = new WebProxy("http://proxy1:3128/");
proxyObject.Credentials = new System.Net.NetworkCredential("archowdhury", "Secure*45", "corp");
webClient.Proxy = proxyObject;//if I don't give webClient.Proxy-it returns Error 407-ProxyAuthentication Required!
result = webClient.DownloadString(url);
//result = webClient.DownloadString("http://www.google.com");//just to check with google itself,still the same error



Match m = Regex.Match(result, "(?<=<div id=result_box dir=\"ltr\">)(.*?)(?=</div>)");

if (m.Success)
result = m.Value;

return result;
}
Manfred Rudolf Bihy 30-Nov-10 2:53am    
Sounds like an issue I had once. The account information used by the proxy was NOT the same as my windows logon account but some LDAP account. You may have to ask your network administrator what account information you need to authenticate yourself to the proxy.
avishekrc 30-Nov-10 2:54am    
Hi Manfred,
Please find below my comments as another answer.
avishekrc 30-Nov-10 2:57am    
Sorry,I did not check your comment,after discussing with Netwrok Guy,will let you know.Please ignore my answer below.
Hi Manfred,
Thanks for your time and help.But unfortunately it is giving the following error:"The Remote server returned an error:407:Proxy Authentication Required!"
I am running it in IE7,and I checked in the following path:Tools->internet Options->Connections->LAN Settings,and there I found that "use a proxy server for your LAN" is checked and ADdress being proxy1 and port no being 3128.
Am I missing something?
This is how my new function looks like as suggested by you:
public static string TranslateText(string input, string languagePair, Encoding encoding)
    {
      string url = String.Format("http://www.google.com/translate_t?hl=en&amp;ie=UTF8&amp;text={0}&amp;langpair={1}", input, languagePair);
      string result = String.Empty;
      WebClient webClient = new WebClient();     
          webClient.Encoding = encoding;
          NetworkCredential netcredit = new NetworkCredential("archowdhury", "Secure*45", "corp");
          webClient.Credentials = netcredit;
          WebProxy proxyObject = new WebProxy("http://proxy1:3128/");
          proxyObject.Credentials = new System.Net.NetworkCredential("archowdhury", "Secure*45", "corp");
          webClient.Proxy = proxyObject;
          result = webClient.DownloadString(url);
          

      Match m = Regex.Match(result, "(?&lt;=&lt;div id=result_box dir=\"ltr\"&gt;)(.*?)(?=&lt;/div&gt;)");

      if (m.Success)
        result = m.Value;

      return result;
    }
 
Share this answer
 
Comments
Manfred Rudolf Bihy 30-Nov-10 2:57am    
Please do NOT post additions or modifications to your question as an answer. Modifiy your original question by adding another paragraph with the changes and mark them as being a modification.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900