Charles Cavalcante
Charles Cavalcante

Reputation: 1608

No connection could be made because the target machine actively refused it

I'm trying to do a web request from a webpage and I'm having problem on production server.

The web application is a intranet web site with this configuration on web.config:

<authentication mode="Windows" />

I use it to get the domain user authentication.

Now I'm trying to load a web page to parse some information, I used the code below, the error appears on the second line:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

The error message is:

No connection could be made because the target machine actively refused it 64.233.163.104:80.

Stack Trace:

[SocketException (0x274d): No connection could be made because the target machine actively refused it 64.233.163.104:80]
   System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +269
   System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +649

[WebException: Unable to connect to the remote server]
   System.Net.HttpWebRequest.GetResponse() +1126
   Fumagalli.Insight.Admin.Teste.Page_Load(Object sender, EventArgs e) +606
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +42
   System.Web.UI.Control.OnLoad(EventArgs e) +132
   System.Web.UI.Control.LoadRecursive() +66
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428

This error don't occur with all URLs that I have tryed, I can get the response from www.yahoo.com but not from www.google.com for example.

On my PC (Windows 7) everything allways works, I can get response from any URL using the same authentication mode (with domain controller), the problem only occur on the production server (Windows Server 2008).

Anyone has a ideia?

Upvotes: 3

Views: 14930

Answers (2)

Prozaker
Prozaker

Reputation: 354

I had a problem like this, it turned out that we are behind a proxy, that could be the issue, I know its a long shot, but it just might work

I copied the proxy data from my browser, just replace the proxy string with your proxy (don't forget the port).

WebProxy webproxy = new WebProxy("http://192.168.4.8:8080");
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Proxy = webproxy;

Upvotes: 5

KeithS
KeithS

Reputation: 71565

The server may actively refuse a connection for other reasons than authentication. Most of these other reasons have to do with the form of the request and the port on which it is received. A mismatch, or a closed port, will result in a refused connection. In this case, however, an HttpRequest made on Google's front door should get you a response. I would guess that Google may inspect the WebAgent header information included in your HttpRequest; your application looks like a .NET application and presents itself to Google as such. I imagine Google has taken steps to reduce DOS attacks from simple .NET applications like yours infinitely looping over a GetResponse() call. You may need to impersonate another browser, like IE 8, by specifying a custom WebAgent header string.

Upvotes: 1

Related Questions