Reputation: 4254
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/>
> HELO verify-email.org<br/>
250 SN1NAM01FT022.mail.protection.outlook.com Hello [verify-email.org]<br/>
> MAIL FROM: <[email protected]><br/>
=250 2.1.0 Sender OK<br/>
> RCPT TO: <[email protected]><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>
Upvotes: 1
Views: 1434
Reputation: 4254
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>
Upvotes: 1