httpClient----java 기반
if (response.getStatusLine ().getStatusCode () != 200) {
get.abort();
return null;
}
HttpGet 작업:
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://www.apache.org/");
System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
System.out.println("----------------------------------------");
InputStream inSm = entity.getContent();
Scanner inScn = new Scanner(inSm);
while (inScn.hasNextLine()) {
System.out.println(inScn.nextLine());
}
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
response 응답 내용 부분을 읽을 수도 있고 httpcore-4.1.2를 빌릴 수도 있습니다.jar 안의
public class Demo1 {
/**
* get www.apache.org
*
* @param args
*/
public static void main(String[] args) {
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
try {
// httpUriRequest
HttpGet httpGet = new HttpGet("http://www.apache.org/");
System.out.println("uri=" + httpGet.getURI());
// get
HttpResponse httpResponse = httpClient.execute(httpGet);
//
HttpEntity httpEntity = httpResponse.getEntity();
//
System.out.println(httpResponse.getStatusLine());
if (httpEntity != null) {
//
long length = httpEntity.getContentLength();
//
String content = EntityUtils.toString(httpEntity);
System.out.println("Response content length:" + length);
System.out.println("Response content:" + content);
}
//
httpGet.abort();
} catch (Exception e) {
e.printStackTrace();
} finally {
// ,
httpClient.getConnectionManager().shutdown();
}
}
}
분석 요청 패키지의 6개 헤더 정보:
httpget.setHeader("Accept-Encoding", "gzip, deflate") 부호가 발생할 수 있음
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
HttpClient httpClient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://www.iteye.com");
httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpget.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1)");
httpget.setHeader("Accept-Encoding", "gzip, deflate");
httpget.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
httpget.setHeader("Host", "www.iteye.com");
httpget.setHeader("Connection", "Keep-Alive");
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
System.out.println("----------------------------------------");
InputStream inSm = entity.getContent();
Scanner inScn = new Scanner(inSm);
while (inScn.hasNextLine()) {
System.out.println(inScn.nextLine());
}
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort();
} catch (Exception e) {
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
}
}
HttpPost 작업:
public class Demo2 {
/**
* post ,
*
* @param args
*/
public static void main(String[] args) {
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:86/test1Servlet");
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
formParams.add(new BasicNameValuePair("param1", "***"));
UrlEncodedFormEntity urlEncodedFormEntity;
try {
urlEncodedFormEntity = new UrlEncodedFormEntity(formParams, "UTF-8");
httpPost.setEntity(urlEncodedFormEntity);
System.out.println("execurting request:" + httpPost.getURI());
HttpResponse httpResponse = null;
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String content = EntityUtils.toString(httpEntity, "UTF-8");
System.out.println("Response content:" + content);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// ,
httpClient.getConnectionManager().shutdown();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Exception Class에서 에러 코드 해석 ~초기초편~직장에서 C# 프로젝트가 내뿜는 오류 코드를 구문 분석하고 오류의 위치를 확인하기 위해 Exception class를 활용할 수 있었습니다. 지금까지 Exception Class 에 대해서 별로 파악할 수 없었기 때...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.