'httpclient'에 해당되는 글 5건

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

[HttpClient] Timeout 설정 예시

ITWeb/개발일반 2017.02.07 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);


저작자 표시 비영리 변경 금지
신고
Trackback 0 : Comment 0

[HttpAsyncClient] example : AsyncClientHttpExchangeFutureCallback

ITWeb/개발일반 2016.09.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");
}


저작자 표시 비영리 변경 금지
신고
Trackback 0 : Comment 0

[HttpClient] Async Http Request Docs.

ITWeb/개발일반 2016.09.28 10:23


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


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


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

저작자 표시 비영리 변경 금지
신고
Trackback 0 : Comment 0

[httpclient] 3.x to 4.x

ITWeb/개발일반 2014.01.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);




신고
tags : httpclient
Trackback 0 : Comment 0

[HttpClient] Request Post Data Raw 사용.

ITWeb/개발일반 2013.08.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

신고
tags : httpclient, POST, RAW
Trackback 0 : Comment 0