尝试从URL下载字符串时连接已关闭

本文关键字:连接 字符串 URL 下载 | 更新日期: 2023-09-27 17:52:13

我想把一个字符串从一种语言翻译成另一种语言。我试过这个代码:

        input = clipoard_word;
        string languagePair = "en|bn";
        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;
        try
        {
             result = webClient.DownloadString(url);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        result = result.Substring(result.IndexOf("<span title='"") + "<span title='"".Length);
        result = result.Substring(result.IndexOf(">") + 1);
        result = result.Substring(0, result.IndexOf("</span>"));
        result = WebUtility.HtmlDecode(result.Trim());
        label1.Text = result;

但是我得到了一个异常:

    result = webClient.DownloadString(url);

错误信息显示:

底层连接已关闭;发生了意想不到的错误On a receive

这里出了什么问题,我该如何解决?N:B:我不想在这里使用谷歌翻译api。

尝试从URL下载字符串时连接已关闭

你好,我终于找到解决办法了

你需要把它添加到你的配置文件

 <system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
      <proxy />
      <bypasslist/>
      <module/>
    </defaultProxy>
  </system.net>

问题是服务器简单地使用IE的代理设置来执行DownloadString调用。我的本地IIS(7.0)要求必须正确配置代理程序,以使服务器端http访问正常工作。这可以通过web.config中的一些设置来完成: