【问题标题】:WebClient.DownloadString fails in website, works otherwiseWebClient.DownloadString 在网站上失败,否则工作
【发布时间】:2016-03-01 14:32:41
【问题描述】:

在我们的 MVC 5 站点中,我们有一个 VerifyEmail 方法,它可以联系服务以检查电子邮件是否存在。我们进行基本的初步检查,以确保它看起来像一封有效的电子邮件等,然后我们将其传递给服务。

我们正在使用WebClient.DownloadString() 函数。当我们导航到适当的 View 并触发该方法时,我们会收到以下错误:

调试时:

<h2>Bad Request - Invalid Hostname</h2>
<hr><p>HTTP Error 400. The request hostname is invalid.</p>

当我们不调试时,我们得到这个:

System.Net.WebException: 无法连接到远程服务器 ---> System.Net.Sockets.SocketException:无法建立连接,因为目标机器主动拒绝它 127.0.0.1:63595
在 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
在 System.Net.ServicePoint.ConnectSocketInternal(布尔连接失败、套接字 s4、套接字 s6、套接字和套接字、IPAddress 和地址、ConnectSocketState 状态、IAsyncResult asyncResult、异常和异常)
--- 内部异常堆栈跟踪结束 ---

如果我从 LinqPad 调用方法 (VerifyEmailOrgEmailVerificationClient.VerifyEmail),它就可以工作!!!

我明白了:

MX record about emailserver.com exists.<br/>
Connection succeeded to emailserver-com.mail.protection.outlook.com SMTP.<br/>
220 SN1NAM01FT022.mail.protection.outlook.com Microsoft ESMTP MAIL Service ready at Mon, 29 Feb 2016 21:08:50 +0000<br/>
&gt; HELO verify-email.org<br/>
250 SN1NAM01FT022.mail.protection.outlook.com Hello [verify-email.org]<br/>
&gt; MAIL FROM: &lt;check@verify-email.org&gt;<br/>
=250 2.1.0 Sender OK<br/>
&gt; RCPT TO: &lt;redacted@emailserver.com&gt;<br/>
=250 2.1.5 Recipient OK<br/>

这就是我们应该得到的。因此,代码在 LinqPad 中有效,但当我们将其称为网站时则无效。我们将一个测试方法放在一起并得到相同的结果。通过我们的测试,我们只是试图从WebClient.DownloadString() 获得响应。即使这样,我们也会得到一个错误。

这是我们的测试方法:

using System;
using System.IO;
using System.Net;
using System.Web.Mvc;
using PublicationSystem.ViewModels;

namespace TestWebClient.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var newModel = new TestViewModel();

            using (var webClient = new WebClient())
            {
                webClient.Encoding = System.Text.Encoding.UTF8;
                webClient.Headers["Content-Type"] = "application/json;charset=UTF-8";

                var requestUrl = string.Empty;// GetRequestUrl(email);
                try
                {
                    requestUrl =
                        "http://api.verify-email.org/api.php?usr=igiglobal&pwd=emailsareweak&check=mkenyon@mkptechnologies.com";
                    newModel.ResponseString = webClient.DownloadString(requestUrl);

                }
                catch (WebException exception)
                {
                    string responseText;

                    if (exception.Response != null)
                    {
                        using (var reader = new StreamReader(exception.Response.GetResponseStream()))
                        {
                            responseText = reader.ReadToEnd();
                        }
                        newModel.ResponseString = responseText;
                    }
                    else
                    {
                        newModel.ResponseString = exception.ToString();
                    }
                }
                catch (Exception exception)
                {
                    newModel.ResponseString = exception.ToString();
                }
            }

            return View(newModel);
        }

我们的 AppPool 是为 .Net 4.0 设置的。该应用程序为 .Net 4.5 编译。我们正在使用 MVC 5。

知道为什么我们的方法,特别是 WebClient.DownloadString() 在我们运行站点时会失败,但如果我们从 LinqPad 调用代码就可以工作吗?

更新:我使用 Visual Studio 2013 创建了一个新的 MVC 项目,就像这个项目一样。在新的测试项目中,我粘贴了相同的代码,并尝试让引用尽可能完全匹配。我的代码在测试项目中工作。所以我的项目设置方式阻止了WebClient.DownloadString()。使用 Fiddler,它看起来甚至没有发送请求。

在测试 MVC 站点时是否有可以阻止 WebClient.DownloadString() 的设置?

更新 2: 在 web.config 我发现了这个。会是这样吗?

<system.net>
  <defaultProxy>
    <proxy proxyaddress="http://localhost:63595/" />
  </defaultProxy>
</system.net>

【问题讨论】:

  • 127.0.0.1:63595 是您希望请求通过的代理/服务地址吗? - 是代理吗?
  • 没有。当我在调试模式下运行时,它是 localhost:63595/,但我传入了一个 URL,例如 www.google.comhttp://api.verify-email.org/api.php?usr=userName&amp;pwd=passWord&amp;check=none@nowhere.com。似乎 WebClient.DownloadString() 甚至都没有尝试。没有代理,不应该有。
  • 嗯,不过,也许您的主机在 localhost:63595 上配置了 DNS 服务器,但它不工作?
  • 生产中的堆栈跟踪是否包含提到 127.0.0.1:63595 的错误?如果是这样,它看起来像是试图通过代理出去。尝试将webClient = new WebProxy(); 设置为重置
  • 听起来可能是防病毒软件或类似的东西干扰了请求(通过其他人提到的代理捕获它)。

标签: c#


【解决方案1】:

在 web.config 我发现了这个。评论它让WebClient.DownloadString() 再次工作。希望这可以帮助某人。我将不得不对此进行更多研究。

<system.net>
  <defaultProxy>
    <proxy proxyaddress="http://localhost:63595/" />
  </defaultProxy>
</system.net>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    相关资源
    最近更新 更多