'httpclient'에 해당되는 글 6건

  1. 2019.11.05 [httpcomponents-client] CloseableHttpClient - Accept Encoding
  2. 2017.02.07 [HttpClient] Timeout 설정 예시
  3. 2016.09.29 [HttpAsyncClient] example : AsyncClientHttpExchangeFutureCallback
  4. 2016.09.28 [HttpClient] Async Http Request Docs.
  5. 2014.01.24 [httpclient] 3.x to 4.x
  6. 2013.08.26 [HttpClient] Request Post Data Raw 사용.

[httpcomponents-client] CloseableHttpClient - Accept Encoding

ITWeb/개발일반 2019. 11. 5. 10:53

RESTful 통신을 많이 하면서 httpclient 를 활용이 높습니다.

제가 사용하고 있는 httpclient 중에 CloseableHttpClient 가 있는데 이 클라이언트의 경우 Accept Encoding 설정이 기본적으로 enable 되어 있습니다.

그래서 기억력을 돕기 위해 또 기록해 봅니다.

 

참고 이전 글)

https://jjeong.tistory.com/1369

 

HttpClientBuilder.java)

public CloseableHttpClient build() {
... 중략 ...
            if (!contentCompressionDisabled) {
                if (contentDecoderMap != null) {
                    final List<String> encodings = new ArrayList<String>(contentDecoderMap.keySet());
                    Collections.sort(encodings);
                    b.add(new RequestAcceptEncoding(encodings));
                } else {
                    b.add(new RequestAcceptEncoding());
                }
            }
            if (!authCachingDisabled) {
                b.add(new RequestAuthCache());
            }
            if (!cookieManagementDisabled) {
                b.add(new ResponseProcessCookies());
            }
            if (!contentCompressionDisabled) {
                if (contentDecoderMap != null) {
                    final RegistryBuilder<InputStreamFactory> b2 = RegistryBuilder.create();
                    for (final Map.Entry<String, InputStreamFactory> entry: contentDecoderMap.entrySet()) {
                        b2.register(entry.getKey(), entry.getValue());
                    }
                    b.add(new ResponseContentEncoding(b2.build()));
                } else {
                    b.add(new ResponseContentEncoding());
                }
            }
... 중략 ...
    }

RequestAcceptEndocing.java)

...중략...
    public RequestAcceptEncoding(final List<String> encodings) {
        if (encodings != null && !encodings.isEmpty()) {
            final StringBuilder buf = new StringBuilder();
            for (int i = 0; i < encodings.size(); i++) {
                if (i > 0) {
                    buf.append(",");
                }
                buf.append(encodings.get(i));
            }
            this.acceptEncoding = buf.toString();
        } else {
            this.acceptEncoding = "gzip,deflate";
        }
    }
...중략...

요청 하는 Client 에서 Server 로 콘텐츠를 압축해서 전송해줘 해야 압축해서 전송을 해주게 되는 내용입니다.

아무리 서버에서 압축 전송이 가능 하도록 설정을 했어도 요청을 하지 않으면 그냥 plain/text 로 넘어 올 수 밖에 없습니다.

 

참고문서)

https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Accept-Encoding

 

Accept-Encoding

Accept-Encoding 요청 HTTP 헤더는, 보통 압축 알고리즘인, 클라이언트가 이해 가능한 컨텐츠 인코딩이 무엇인지를 알려줍니다. 컨텐츠 협상을 사용하여, 서버는 제안된 내용 중 하나를 선택하고 사용하며 Content-Encoding 응답 헤더를 이용해 선택된 것을 클라이언트에게  알려줍니다.

developer.mozilla.org

 

:

[HttpClient] Timeout 설정 예시

ITWeb/개발일반 2017. 2. 7. 13:41

HttpClient를 사용하면서 간혹 read timeout 이나 connection timeout 등이 발생 할 때가 있습니다.

그럴때 값을 조정해서 사용하시면 됩니다.


- 문서링크

setConnectionRequestTimeout

public RequestConfig.Builder setConnectionRequestTimeout(int connectionRequestTimeout)


setConnectTimeout

public RequestConfig.Builder setConnectTimeout(int connectTimeout)


setSocketTimeout

public RequestConfig.Builder setSocketTimeout(int socketTimeout)



아래 예시는 10초로 설정한 내용입니다.

HttpPost request = new HttpPost(url);


RequestConfig requestConfig = RequestConfig.custom()

  .setSocketTimeout(10*1000)

  .setConnectTimeout(10*1000)

  .setConnectionRequestTimeout(10*1000)

  .build();


request.setConfig(requestConfig);


:

[HttpAsyncClient] example : AsyncClientHttpExchangeFutureCallback

ITWeb/개발일반 2016. 9. 29. 16:35

Concurrent Request 를 보낼 일이 있어서 샘플 코드 투척해 봅니다.

제가 작성한건 아니고 그냥 httpcomponents-asyncclient 에 있는 example 입니다.


Main Method)

public static void main(final String[] args) throws Exception {
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(3000)
.setConnectTimeout(3000).build();
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(requestConfig)
.build();
try {
httpclient.start();
final HttpGet[] requests = new HttpGet[] {
new HttpGet("http://www.apache.org/"),
new HttpGet("https://www.verisign.com/"),
new HttpGet("http://www.google.com/")
};
final CountDownLatch latch = new CountDownLatch(requests.length);
for (final HttpGet request: requests) {
httpclient.execute(request, new FutureCallback<HttpResponse>() {

@Override
public void completed(final HttpResponse response) {
latch.countDown();
System.out.println(request.getRequestLine() + "->"
+ response.getStatusLine());
}

@Override
public void failed(final Exception ex) {
latch.countDown();
System.out.println(request.getRequestLine() + "->" + ex);
}

@Override
public void cancelled() {
latch.countDown();
System.out.println(request.getRequestLine() + " cancelled");
}

});
}
latch.await();
System.out.println("Shutting down");
} finally {
httpclient.close();
}
System.out.println("Done");
}


:

[HttpClient] Async Http Request Docs.

ITWeb/개발일반 2016. 9. 28. 10:23


async http client 기능 구현을 위해 문서 keep 합니다.


https://hc.apache.org/httpcomponents-asyncclient-dev/quickstart.html


https://github.com/AsyncHttpClient/async-http-client

:

[httpclient] 3.x to 4.x

ITWeb/개발일반 2014. 1. 24. 14:52

API 구조가 많이 바뀌었습니다.

계속 3.x 쓰다가 4.x 로 갈아 탈라고 wrapper class 만들다 보니 참고할 만한 내용이 필요해서 올려 봅니다.

http://hc.apache.org/httpcomponents-client-4.3.x/index.html


[요약]

- xxxMethod 는 Httpxxx

- http status code 는 httpresponse.getStatusLine().getStatusCode()

- response body content 는 httpresponse.getEntity().getContent()

로 바뀌었습니다.


httpclient 3.x to 4.x

http://debuguide.blogspot.in/2013/01/quick-guide-for-migration-of-commons.html


Commons HttpClient 3.x
HttpComponents HttpClient 4.x
import
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient. MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient. UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
HttpClient 
HttpClient client = new HttpClient(connectionManager);
DefaultHttpClient client = new DefaultHttpClient();



proxy configuration
client.getHostConfiguration().setProxy( proxyHost, proxyPort);
client.getState().setAuthenticationPreemptive( true);
Credentials cred = new  UsernamePasswordCredentials( proxyUser, proxyPassword);
client.getState().setProxyCredentials( AuthScope.ANY_HOST, proxyHost, cred)
client.getCredentialsProvider(). setCredentials(
 new AuthScope(proxyHost, proxyPort),
 new UsernamePasswordCredentials( proxyUser, proxyPassword));
HttpHost proxy = new HttpHost(proxyHost proxyPort);
client.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy);






POST
PostMethod post = new PostMethod();
post.setPath(url.getFile());
HttpPost post = new HttpPost(url.getFile());



POST Header
post.setRequestHeader(key, (String) headers.get(key));
post.addHeader(key, (String) headers.get(key));



POST Body
post.setRequestBody(message);
StringEntity messageEntity = new StringEntity( message,
 ContentType.create("text/plain", "UTF-8"));
      post.setEntity(messageEntity);



Execute POSt
client.executeMethod(post);
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
HttpResponse httpResponse = client.execute(targetHost, post);



Response Header
Header[] rspHeaders = post.getResponseHeaders();
Header[] rspHeaders = httpResponse.getAllHeaders();



Response Body
String responseMsg = post.getResponseBodyAsString()
StringBuffer buffer = new StringBuffer();
      BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String dataLine = null;
while((dataLine = reader.readLine()) != null){
            buffer.append(dataLine);
}
String responseMsg =    buffer.toString();



GET
GetMethod getMethod = new GetMethod();
getMethod.setPath(url.getFile());
HttpGet httpGet = new HttpGet(url.getFile());



GET Header
getMethod.setRequestHeader(key, value);
httpGet.addHeader(key, value);



Execute GET
client.executeMethod(getMethod);
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
HttpResponse httpResponse = client.execute( targetHost, httpGet);




:

[HttpClient] Request Post Data Raw 사용.

ITWeb/개발일반 2013. 8. 26. 17:09

HttpClient 를 이용해서 Post, Get 관련 기능 구현 시 아래 링크를 참고 하시면 좋습니다.

http://hc.apache.org/httpcomponents-client-ga/examples.html


그리고 raw 방식으로 구현 하고 싶으실 경우는 아래 method 를 이용하셔야 합니다.

PostMethod.setRequestEntity()

Parameter 로 사용 가능 한건 아래 링크 참고 하세요.

http://hc.apache.org/httpclient-legacy/apidocs/org/apache/commons/httpclient/methods/RequestEntity.html

: