[httpclient] 3.x to 4.x
ITWeb/개발일반 2014. 1. 24. 14:52API 구조가 많이 바뀌었습니다.
계속 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);
|