gop
gop

Reputation: 2200

Connection rejected by server

I am using HTTP post connection and my app periodically makes connections to the server. After a certain point I can't connect to the server and I get these exceptions for all of my connections:

java.net.ConnectException;failed to connect to www.somedomain.com/xxx.xxx.xxx.x (port 80): connect failed: ENETUNREACH (Network is unreachable)

org.apache.http.conn.HttpHostConnectException;Connection to http://www.somedomain.com refused

java.net.UnknownHostException;Unable to resolve host "www.somedomain.com": No address associated with hostname

I suspect that the server might be blocking me after a certain point. Any ideas why this might be happening? After I reinstall the app the connection is still blocked.

EDIT: here is the code for sending http requests

public String send() throws ClientProtocolException, IOException {
    InputStream is = null;
    DefaultHttpClient httpclient = null;
    HttpResponse response = null;
    try {
        System.setProperty("http.keepAlive", "false");
        httpclient = new DefaultHttpClient();
        HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false);
        HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {

            public boolean retryRequest(IOException exception, int executionCount,
                    HttpContext context) {
                // retry a max of 5 times
                if(executionCount >= 5){
                    return false;
                }
                if(exception instanceof NoHttpResponseException){
                    return true;
                } else if (exception instanceof ClientProtocolException){
                    return true;
                } 
                return false;
            }
        };
        httpclient.setHttpRequestRetryHandler(retryHandler);
        String proxyHost = android.net.Proxy.getDefaultHost();
        int proxyPort = android.net.Proxy.getDefaultPort();
        // Set Proxy params of client, if they are not the standard
        if (proxyHost != null && proxyPort > 0) {
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            httpclient.getParams().setParameter(
                    ConnRoutePNames.DEFAULT_PROXY, proxy);
        }
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(new UrlEncodedFormEntity(qData));
        response = httpclient.execute(httppost);
        is = response.getEntity().getContent();
        return inputStreamToString(is);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (Throwable e) {
        }
        try {
            if (response != null && response.getEntity() != null) {
                response.getEntity().consumeContent();
            }
        } catch (Throwable e) {
        }
        try {
            if (httpclient != null
                    && httpclient.getConnectionManager() != null) {
                httpclient.getConnectionManager().shutdown();
            }
        } catch (Throwable e) {
        }
    }
}

Upvotes: 0

Views: 1320

Answers (1)

Szocske
Szocske

Reputation: 7661

You can use dig or nslookup to verify the DNS server. You can configure a known good DNS server in your OS like 8.8.8.8 . The exception error message might also be misleading: try capturing the network traffic with wireshark, then you'll see whether the DNS query returns nothing at all, "not found", or is the DNS fine, but the server host rejecting your request.

You can also bypass DNS uncertainty by adding www.somedomain.com to your hosts file with its IP address.

Upvotes: 1

Related Questions