WebClient.DownloadString(),无法连接远程服务器 [英] WebClient.DownloadString(),Unable to connect Remote Server

查看:276
本文介绍了WebClient.DownloadString(),无法连接远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想使用Google API从英语翻译成马来语。

我有以下代码:

Hi ,
I want to translate from English to Malay using Google API.
I have the following code:

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;
    }



执行此操作后:出现以下错误:无法连接远程服务器,详细错误消息如下:

由于目标机器主动拒绝它,因此无法建立连接209.85.231.104:80

我哪里出错?请帮忙me。


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.

推荐答案

如果您没有在webclient上设置凭据,会发生什么。您很可能需要凭据代理,但不是谷歌翻译。引起我注意的另一件事是你没有明确指定代理服务器地址。





修改





使用它来创建Web代理:

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



你必须替换WebProxy构造器中代理服务器的名称,我假设端口80是代理端口。您将不得不询问您的网络管理员要使用的名称和端口。



干杯



Manfred


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


嗨Manfred,

感谢您的时间和帮助。但遗憾的是它出现以下错误:远程服务器返回错误:407 :需要代理身份验证!

我在IE7中运行它,我检查了以下路径:工具 - >互联网选项 - >连接 - >局域网设置,以及在那里我发现为你的局域网使用代理服务器被检查,ADdress是proxy1,端口号是3128.

我错过了什么?

这是我的意思新函数看起来像你建议的那样:

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;
    }


这篇关于WebClient.DownloadString(),无法连接远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆