2016-03-01 25 views
1

在我們的MVC 5個網站,我們有一個VerifyEmail方法接觸服務檢查電子郵件的存在。我們進行基本的初步檢查,以確保它看起來像一個有效的電子郵件等,然後我們將它傳遞給服務。WebClient.DownloadString在網站出現故障,否則工作

我們使用WebClient.DownloadString()功能。當我們導航到相應的查看和觸發方法,我們得到這些錯誤:

調試時:

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

當我們不進行調試,我們得到這樣的:

系統。 Net.WebException:無法連接到遠程服務器---> System.Net.Sockets.SocketException:無連接可以作出,因爲目標機器積極地拒絕它127.0.0.1:63595
在的System.Net.Sockets。 Socket.DoConnect(EndPoint endPointS napshot,爲SocketAddress的SocketAddress)
在System.Net.ServicePoint.ConnectSocketInternal(布爾connectFailure,插座s4中,插座S6中,插座&插座,ip地址&地址,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;[email protected]&gt;<br/> 
=250 2.1.0 Sender OK<br/> 
&gt; RCPT TO: &lt;[email protected]&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&[email protected]"; 
        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); 
     } 

我們的應用程序池設置爲.NET 4.0。該應用程序爲.Net 4.5編譯。我們使用MVC 5

任何想法,爲什麼我們的方法,特別是WebClient.DownloadString(),而我們正在運行的站點發生故障,但如果我們稱之爲從LinqPad代碼工作?

更新:我用Visual Studio 2013創建了一個新的MVC項目,就像這個項目一樣。在新的測試項目中,我粘貼了相同的代碼,並試圖使引用盡可能匹配。我的代碼在測試項目中工作。因此,我的項目設置方式阻礙了WebClient.DownloadString()。使用Fiddler,看起來它甚至不會發送請求。

是否有可能阻止WebClient.DownloadString()同時測試的MVC網站的設置?

更新2:在web.config中我找到了這個。這可能嗎?

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

127.0.0.1:63595是您希望請求路由的代理/服務地址? - 它是代理嗎? –

+0

沒有。當我在調試模式下運行時,它是localhost:63595 /但我傳遞了一個URL,如「www.google.com」或http://api.verify-email.org/api.php?usr=userName&pwd =密碼和校驗爲無@ nowhere.com'。它幾乎看起來像WebClient.DownloadString()甚至沒有嘗試。沒有代理,不應該有。 –

+1

嗯,雖然,也許你的主機有一個DNS服務器配置localhost:63595,它不工作? – Gusman

回答

1

在web.config中我發現了這個。評論它,讓WebClient.DownloadString()再次工作。希望這有助於某人。我必須對此做更多的研究。

<system.net> 
    <defaultProxy> 
    <proxy proxyaddress="http://localhost:63595/" /> 
    </defaultProxy> 
</system.net> 
相關問題