1

In our MVC 5 site we have a VerifyEmail 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.

We're using a WebClient.DownloadString() function. When we navigate to the appropriate View and trigger the method, we get these errors:

When debugging:

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

When we're not debugging, we get this:

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
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---

If I call the method (VerifyEmailOrgEmailVerificationClient.VerifyEmail) from LinqPad it works!!!

I get this:

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/>

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 WebClient.DownloadString(). Even like that we get an error.

Here's our test method:

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

Our AppPool is set for .Net 4.0. The app compiles for .Net 4.5. We're using MVC 5.

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?

UPDATE: 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 WebClient.DownloadString(). Using Fiddler, it looks like it doesn't even send the request out.

Is there a setting that could block WebClient.DownloadString() while testing an MVC site?

UPDATE 2: In the web.config I found this. Could this be it?

<system.net>
  <defaultProxy>
    <proxy proxyaddress="http://localhost:63595/" />
  </defaultProxy>
</system.net>
5
  • 1
    127.0.0.1:63595 is the proxy/service address you expect the request to route through? - is it a proxy? Commented Mar 1, 2016 at 14:37
  • No. When I run in debug mode, it's localhost:63595/ but I pass in a URL like www.google.com or http://api.verify-email.org/api.php?usr=userName&pwd=passWord&[email protected]. It almost seems like WebClient.DownloadString() isn't even trying. There is no proxy, there shouldn't be. Commented Mar 1, 2016 at 14:43
  • 1
    Hmmm, a though, maybe your host has a DNS server configured on localhost:63595 and it's not working? Commented Mar 1, 2016 at 14:48
  • Does the stack trace in production include the error mentioning 127.0.0.1:63595 ? If so it looks like its trying to go out via a proxy. Try setting webClient = new WebProxy(); to reset Commented Mar 1, 2016 at 14:54
  • Sounds like maybe an anti-virus software or something like that is interfering with the request (capturing it through a proxy like others have mentioned). Commented Mar 10, 2016 at 14:13

1 Answer 1

1

In the web.config I found this. Commenting it out let WebClient.DownloadString() work again. Hope this helps somebody. I'll have to do more research on this.

<system.net>
  <defaultProxy>
    <proxy proxyaddress="http://localhost:63595/" />
  </defaultProxy>
</system.net>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.