title: WebClient.DownloadString fails in website, works otherwise
body: <p>In our MVC 5 site we have a <code>VerifyEmail</code> method that contacts a service to check that an email exists. We do the basic preliminary checks, to make sure it looks like a valid email, etc, then we pass it to the service.</p>
  
  <p>We're using a <code>WebClient.DownloadString()</code> function. When we navigate to the appropriate View and trigger the method, we get these errors:</p>
  
  <p>When debugging:</p>
  
  <pre><code>&lt;h2&gt;Bad Request - Invalid Hostname&lt;/h2&gt;
  &lt;hr&gt;&lt;p&gt;HTTP Error 400. The request hostname is invalid.&lt;/p&gt;
  </code></pre>
  
  <p>When we're not debugging, we get this:</p>
  
  <blockquote>
    <p>System.Net.WebException: Unable to connect to the remote server ---> 
    System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:63595<br>
      at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)<br>
      at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket&amp; socket, IPAddress&amp; address, ConnectSocketState state, IAsyncResult asyncResult, Exception&amp; exception)<br>
      --- End of inner exception stack trace ---</p>
  </blockquote>
  
  <p>If I call the method (<code>VerifyEmailOrgEmailVerificationClient.VerifyEmail</code>) from LinqPad it works!!!</p>
  
  <p>I get this:</p>
  
  <pre><code>MX record about emailserver.com exists.&lt;br/&gt;
  Connection succeeded to emailserver-com.mail.protection.outlook.com SMTP.&lt;br/&gt;
  220 SN1NAM01FT022.mail.protection.outlook.com Microsoft ESMTP MAIL Service ready at Mon, 29 Feb 2016 21:08:50 +0000&lt;br/&gt;
  &amp;gt; HELO verify-email.org&lt;br/&gt;
  250 SN1NAM01FT022.mail.protection.outlook.com Hello [verify-email.org]&lt;br/&gt;
  &amp;gt; MAIL FROM: &amp;lt;check@verify-email.org&amp;gt;&lt;br/&gt;
  =250 2.1.0 Sender OK&lt;br/&gt;
  &amp;gt; RCPT TO: &amp;lt;redacted@emailserver.com&amp;gt;&lt;br/&gt;
  =250 2.1.5 Recipient OK&lt;br/&gt;
  </code></pre>
  
  <p>That's what we should get. So, the code works from LinqPad, but not when we call it as a website. We put a test method together and get the same results. With our test, we're just trying to get a response from <code>WebClient.DownloadString()</code>. Even like that we get an error.</p>
  
  <p>Here's our test method:</p>
  
  <pre><code>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&amp;pwd=emailsareweak&amp;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);
          }
  </code></pre>
  
  <p>Our AppPool is set for .Net 4.0. The app compiles for .Net 4.5. We're using MVC 5.</p>
  
  <p>Any idea why our method, specifically the WebClient.DownloadString() fails while we're running the site, but works if we call the code from LinqPad?</p>
  
  <p><strong><em>UPDATE:</em></strong> I created a new MVC project with Visual Studio 2013, just like this project. In the new test project, I pasted the same code, and have tried to get the references to match as exactly as possible. My code works in the test project. So something with the way my project is set up is blocking <code>WebClient.DownloadString()</code>. Using Fiddler, it looks like it doesn't even send the request out.</p>
  
  <p>Is there a setting that could block <code>WebClient.DownloadString()</code> while testing an MVC site?</p>
  
  <p><strong><em>UPDATE 2:</em></strong> In the web.config I found this. Could this be it?</p>
  
  <pre><code>&lt;system.net&gt;
    &lt;defaultProxy&gt;
      &lt;proxy proxyaddress="http://localhost:63595/" /&gt;
    &lt;/defaultProxy&gt;
  &lt;/system.net&gt;
  </code></pre>