【问题标题】:Connection Closed while trying to download string from URL尝试从 URL 下载字符串时连接已关闭
【发布时间】:2012-12-26 02:39:37
【问题描述】:

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

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

错误信息说:

底层连接已关闭;发生意外错误 在接收时

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

【问题讨论】:

标签: c# winforms webclient connectionexception


【解决方案1】:

嗨,我终于找到了适合你的解决方案

你需要在你的配置文件中添加这个

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多