21

I have a Auto Complete/type ahead feature on Search for my website. I see that some time their is an exception associated with it. We are using a proxy server.

org.apache.http.conn.HttpHostConnectException: Connection to http://proxy.xyz.com:60 refused    

at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:159)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:108)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
at com.xxx.dd.sone.integration.SearchDAO.getJSONData(SearchDAO.java:60)
at com.xxx.dd.sone.integration.SearchDAO.searchAutoCompleteResults(SearchDAO.java:560)
at com.xxx.dd.sone.presentation.util.SearchAutoCompleteUtil.doGet(SearchAutoCompleteUtil.java:26)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:845)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:352)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:236)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1512)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255)
at weblogic.work.ExecuteRequestAdapter.execute(ExecuteRequestAdapter.java:22)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:147)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:119)

Caused by: java.net.ConnectException: Connection refused

Here is the how i have coded

public HashMap<String, Object> getJSONData(String url)throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams params = httpClient.getParams();
    try {
        HttpConnectionParams.setConnectionTimeout(params, 10000);
        HttpConnectionParams.setSoTimeout(params, 10000);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    HttpHost proxy = new HttpHost(proxy.xyz.com, 60);
    ConnRouteParams.setDefaultProxy(params, proxy);
    URI uri;
    InputStream data = null;
        uri = new URI(url);
        HttpGet method = new HttpGet(uri);
        HttpResponse response=null;
        try {
        response = httpClient.execute(method);
        }catch(Exception e) {
            e.printStackTrace();
            throw e;
        }
        data = response.getEntity().getContent();
    Reader r = new InputStreamReader(data);
    HashMap<String, Object> jsonObj = (HashMap<String, Object>) GenericJSONUtil.fromJson(r);
    return jsonObj;
}

Can any one tell me why i am getting this exception some time only? is this possible that this exception is caused when a search request is made from Android applications as our website don't support a request is being made from android applications

pushya
  • 4,338
  • 10
  • 45
  • 54
  • Is your proxy host running on port 60? If not - that would result in the connection refused message. – Dave G Mar 21 '13 at 11:47
  • It is running on same port. Also this exception don't happen each and every time. some times only. – pushya Mar 21 '13 at 11:51
  • Unfortunately you'd have to look at why the proxy is denying the connection. Possibly an error in the proxy configuration? – Dave G Mar 21 '13 at 11:53
  • That proxy is being used my most of the applications company wide. I don't think their will be any issue with configuration the proxy. Also one question do proxies configuration differs based on different platforms (like Mac/Android) – pushya Mar 21 '13 at 11:57

3 Answers3

39

A "connection refused" error happens when you attempt to open a TCP connection to an IP address / port where there is nothing currently listening for connections. If nothing is listening, the OS on the server side "refuses" the connection.

If this is happening intermittently, then the most likely explanations are (IMO):

  • the server you are talking ("proxy.xyz.com" / port 60) to is going up and down, OR
  • there is something1 between your client and the proxy that is intermittently sending requests to a non-functioning host, or something.

Is this possible that this exception is caused when a search request is made from Android applications as our website don't support a request is being made from android applications.

It seems unlikely. You said that the "connection refused" exception message says that it is the proxy that is refusing the connection, not your server. Besides if a server was going to not handle certain kinds of request, it still has to accept the TCP connection to find out what the request is ... before it can reject it.


1 - For example, it could be a DNS that round-robin resolves the DNS name to different IP addresses. Or it could be an IP-based load balancer.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Just to clear the search request is going to another service provider and we have implemented their service on our website. All we do is send the requests from the consumer and send it to the search service provider and display the info send by them – pushya Mar 21 '13 at 12:00
  • "there is something between your client and the proxy that is intermittently sending requests to a non-functioning host, or something" what do you mean by non functioning host? – pushya Mar 21 '13 at 12:05
  • Some host other than your proxy server that is not expecting those connection requests; i.e. it is not functioning as a proxy ... – Stephen C Mar 21 '13 at 12:13
  • how can we fix this problem ? – eng.ahmed Sep 25 '13 at 10:40
  • I think the problem might also be because their is a auto complete on my search box and the action triggers as soon as user starts typing, So their is a possibility that proxy is refusing the call if it see lot of call happening in fraction of a second. One solution is that to trigger auto complete after user has typed in some minimum characters( like 3). – pushya Dec 20 '13 at 15:59
  • @eng.ahmed - You have to UNDERSTAND what is causing the problem before you can fix it. – Stephen C Nov 04 '14 at 11:14
  • 1
    @pushya - That is possible. And if your theory is correct, then your proposed fix might work. However, you need real concrete evidence for that before you start *blaming* the proxy. – Stephen C Nov 04 '14 at 11:18
  • is that possible that the hosted website has no SSL? – Hack Dawg Jan 09 '20 at 02:48
  • Yes. If you attempted to send a request to the https port (443) of a server that did not support https, then you would get a connection refused. – Stephen C Jan 09 '20 at 06:34
1

In my case the issue was a missing 's' in the HTTP URL. Error was: "HttpHostConnectException: Connect to someendpoint.com:80 [someendpoint.com/127.0.0.1] failed: Connection refused" End point and IP obviously changed to protect the network.

Bob Small
  • 113
  • 1
  • 10
1

You must set proxy server for gradle at some time, you can try to change the proxy server ip address in gradle.properties which is under .gradle document

applixy
  • 873
  • 6
  • 5