Total Pageviews

2017/07/09

[HttpClient] Fail to Get Response from a Valid URL

Problem
I am using HttpClient Get method to retrieve the response body of the HTTP method, if any.
During the execution, I fail to get response from a valid URL. 

The code snippet is as follows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package albert.practice.http;

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;

public class HttpSmsClient {

    private final static Logger logger = LoggerFactory.getLogger(HttpSmsClient.class);

    public static String getResponse(String url) throws IOException {
        String responseString = "";
        HttpClient client = new HttpClient();
        
        GetMethod method = new GetMethod(url);
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        int statusCode;
        try {
            statusCode = client.executeMethod(method);            
            if (HttpStatus.SC_OK == statusCode) {
                responseString = IOUtils.toString(method.getResponseBodyAsStream(), Charsets.UTF_8);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            method.releaseConnection();
        }
        logger.info("responseString = " + responseString);
        return responseString;
    }

}



How-To
In my office, I need to specify proxy server before I connect to various services.
Therefore, the source code will be modified as follows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package albert.practice.http;

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;

public class HttpSmsClient {

    private final static Logger logger = LoggerFactory.getLogger(HttpSmsClient.class);

    public static String getResponse(String url, String proxy, String port) throws IOException {
        String responseString = "";
        HttpClient client = new HttpClient();

        HostConfiguration hostConfiguration = client.getHostConfiguration();
        hostConfiguration.setProxy(proxy, Integer.valueOf(port));
        client.setHostConfiguration(hostConfiguration);
        
        GetMethod method = new GetMethod(url);
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        int statusCode;
        try {
            statusCode = client.executeMethod(method);            
            if (HttpStatus.SC_OK == statusCode) {
                responseString = IOUtils.toString(method.getResponseBodyAsStream(), Charsets.UTF_8);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            method.releaseConnection();
        }
        logger.info("responseString = " + responseString);
        return responseString;
    }

}


Maven dependencies are as follows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    <dependencies>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>
    </dependencies>




No comments: