HttpClient 4.3 과 4.3 버 전 이하 버 전 비교
3318 단어 자바httpclient
httpclient
httpclient3.x
HttpClient client = new HttpClient();
//
// client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);
// GET , HTTPS , URL http https
HttpMethodmethod = new GetMethod("http://java.sun.com");
// POST
// HttpMethod method = new PostMethod("http://java.sun.com");
client.executeMethod(method);
//
System.out.println(method.getStatusLine());
//
System.out.println(method.getResponseBodyAsString());
//
method.releaseConnection();
httpclient 4. x 에서 httpclient 4.3 이하
public void getUrl(String url, String encoding) throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, encoding));
System.out.println(reader.readLine());
} catch (Exception e) {
e.printStackTrace();
} finally {
instream.close();
}
}
// .
client.getConnectionManager().shutdown();
}
httpclient 4.3 이상
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public static String getResult(String urlStr) {
CloseableHttpClient httpClient = HttpClients.createDefault();
// HTTP Get
HttpGet httpGet = new HttpGet(urlStr);
//
// RequestConfig requestConfig =
// RequestConfig.custom().setSocketTimeout(TIME_OUT).setConnectTimeout(TIME_OUT).build();
// httpGet.setConfig(requestConfig);
String res = "";
try {
//
HttpResponse getAddrResp = httpClient.execute(httpGet);
HttpEntity entity = getAddrResp.getEntity();
if (entity != null) {
res = EntityUtils.toString(entity);
}
log.info(" " + getAddrResp.getStatusLine());
} catch (Exception e) {
log.error(e.getMessage(), e);
return res;
} finally {
try {
httpClient.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
return res;
}
}
return res;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.