자바 httpclient 시 뮬 레이 션 로그 인
26895 단어 자바
HttpCore 와 HttpClient:HttpCore 는 HTTP 전송 구성 요소 에 있 는 바 텀 패키지 로 HTTP 클 라 이언 트 와 서버 의 개발 을 간소화 할 수 있 습 니 다.HttpClient 는 HTTP 1.1 버 전 으로 HttpCore 패키지 기반 의 구현 입 니 다.이 는 클 라 이언 트 인증,HTTP 상태 관리,HTTP 연결 관리 에 재 활용 가능 한 클 라 이언 트 구성 요 소 를 제공 합 니 다.HttpCore 패키지 의 현재 최신 버 전 은 httpcore-4.2.4 입 니 다.HttpClient 패키지 의 버 전 은 httpclient-4.2.5 입 니 다.HttpCore 패키지 와 HttpClient 패키지 의 차 이 를 알 게 되면 프로그램 에서 어떤 종류의 라 이브 러 리 에 존재 하 는 지 대체적으로 알 아야 합 니 다.예 를 들 어 org.apache.http 패 키 지 는 HttpCore 에 속 하고 org.apache.http.client 패 키 지 는 HttpClient 에 속 합 니 다
HttpClient 의 API 문 서 는 다운로드 한 zip 에 포함 되 어 있 습 니 다.HttpCore 의 API 문 서 는 다음 을 참고 할 수 있 습 니 다.http://hc.apache.org/httpcomponents-core-4.2.x/httpcore/apidocs/index.html
HttpClient 4.2 는 자바 5.0 이상 버 전이 필요 합 니 다.지원 패키지 가 필요 합 니 다(zip 패키지 에 포함 되 어 있 음 다운로드):*Apache HttpComponents HttpCore*Apache Commons Logging*Apache Commons CodecHTML 페이지 의 내용,간단 한 get 응용 프로그램 가 져 오기
// HTML , get
public void grabPageHTML() throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.baidu.com/");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity, "GBK");
// releaseConnection reset, request , 。
// HttpGet ; 。
httpget.releaseConnection();
System.out.println(html);
}
로 컬 에 파일 다운로드(이 시범 에서 인증 코드 그림)
// ( )
public void downloadFile() throws Exception {
String url = "http://www.lashou.com/account/captcha";
String destfilename = "D:\\TDDOWNLOAD\\yz.png";
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
File file = new File(destfilename);
if (file.exists()) {
file.delete();
}
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
try {
FileOutputStream fout = new FileOutputStream(file);
int l = -1;
byte[] tmp = new byte[2048];
while ((l = in.read(tmp)) != -1) {
fout.write(tmp);
}
fout.close();
} finally {
// InputStream HttpEntity , 。
in.close();
}
httpget.releaseConnection();
}
Post 방법,아 날로 그 폼 제출 매개 변 수 를 사이트 에 로그 인하 고 회원 페이지 를 열 어 내용 가 져 오기(세 션 유지)
// Post , 。
// :grabPageHTML/downloadFile, Post 。
public void login2Lashou() throws Exception {
// :
String url = "http://www.lashou.com/account/captcha";
String destfilename = "D:\\TDDOWNLOAD\\yz.png";
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
File file = new File(destfilename);
if (file.exists()) {
file.delete();
}
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
try {
FileOutputStream fout = new FileOutputStream(file);
int l = -1;
byte[] tmp = new byte[2048];
while ((l = in.read(tmp)) != -1) {
fout.write(tmp);
}
fout.close();
} finally {
in.close();
}
httpget.releaseConnection();
// : Post , 、
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" ...");
String yan = br.readLine();
HttpPost httppost = new HttpPost("http://www.lashou.com/account/login/");
List params = new ArrayList();
params.add(new BasicNameValuePair("user_id", "testuser007"));
params.add(new BasicNameValuePair("pwd", "asdfg123"));
params.add(new BasicNameValuePair("yan", yan));
params.add(new BasicNameValuePair("save_user", "on"));
params.add(new BasicNameValuePair("save_pwd", "on"));
params.add(new BasicNameValuePair("sub", " "));
httppost.setEntity(new UrlEncodedFormEntity(params));
response = httpclient.execute(httppost);
entity = response.getEntity();
// Jsoup ,
String postResult = EntityUtils.toString(entity, "GBK");
// Cookie 。
List cookies = ((AbstractHttpClient)httpclient).getCookieStore().getCookies();
for(Cookie cookie: cookies)
System.out.println(cookie);
httppost.releaseConnection();
// : ( )
String memberpage = "http://www.lashou.com/account/orders/";
httpget = new HttpGet(memberpage);
response = httpclient.execute(httpget); // HttpClient!
entity = response.getEntity();
String html = EntityUtils.toString(entity, "GBK");
httpget.releaseConnection();
System.out.println(html);
}
출력:
다운로드 한 인증 코드 에 표 시 된 숫자 를 입력 하 십시오...sbzq...
[version: 0][name: login_name2][value: testuser007][domain: www.lashou.com][path: /][expiry: Mon Sep 09 10:21:19 CST 2013] [version: 0][name: pwd2][value: 4c88a4062736c26572d3ec382868fa2b][domain: lashou.com][path: /][expiry: Mon Sep 09 10:21:19 CST 2013] ?
...
프 록 시 설정
//
public void testProxy() throws Exception {
HttpHost proxy = new HttpHost("127.0.0.1", 8888);
//
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
//
HttpParams params = new BasicHttpParams();
params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpClient httpclient1 = new DefaultHttpClient(params);
}
자주 사용 하 는 HTTP 헤더 설정
// HTTP
public void testBasicHeader() throws Exception {
HttpParams params = new BasicHttpParams();
Collection collection = new ArrayList();
collection.add(new BasicHeader("Accept", "text/html, application/xhtml+xml, */*"));
collection.add(new BasicHeader("Referer", "http://www.sina.com/"));
collection.add(new BasicHeader("Accept-Language", "zh-CN"));
collection.add(new BasicHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"));
collection.add(new BasicHeader("Accept-Encoding", "gzip, deflate"));
params.setParameter(ClientPNames.DEFAULT_HEADERS, collection);
HttpClient httpclient = new DefaultHttpClient(params);
//
}
다 중 스 레 드 프로 그래 밍 의 스 레 드 풀 설정
// ( HttpClient )
public void testConnectionManager() throws Exception {
//
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
cm.setMaxTotal(200); //
cm.setDefaultMaxPerRoute(20); //
HttpHost localhost = new HttpHost("locahost", 80); //
cm.setMaxPerRoute(new HttpRoute(localhost), 30);
//
HttpParams params = new BasicHttpParams();
params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
HttpClient httpclient = new DefaultHttpClient(cm, params);
//
}
HTTP 컨 텍스트 대상 테스트(HttpContext)
// HTTP (HttpContext)
public void testContext() throws Exception {
// ,
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpget = new HttpGet("http://www.baidu.com/");
HttpResponse response = httpclient.execute(httpget, localContext);
// the actual connection to the target server.
HttpConnection conn = (HttpConnection) localContext.getAttribute(
ExecutionContext.HTTP_CONNECTION);
System.out.println("Socket timeout: " + conn.getSocketTimeout());
// the connection target
HttpHost target = (HttpHost) localContext.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);
System.out.println("Final target: " + target);
// the connection proxy, if used
HttpHost proxy = (HttpHost) localContext
.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
if (proxy != null)
System.out.println("Proxy host/port: " + proxy.getHostName() + "/"
+ proxy.getPort());
// the actual HTTP request
HttpRequest request = (HttpRequest) localContext
.getAttribute(ExecutionContext.HTTP_REQUEST);
System.out.println("HTTP version: " + request.getProtocolVersion());
Header[] headers = request.getAllHeaders();
System.out.println("HTTP Headers: ");
for (Header header : headers) {
System.out.println("\t" + header.getName() + ": " + header.getValue());
}
System.out.println("HTTP URI: " + request.getRequestLine().getUri());
// the actual HTTP response
response = (HttpResponse) localContext
.getAttribute(ExecutionContext.HTTP_RESPONSE);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("Content Encoding:" + entity.getContentEncoding());
System.out.println("Content Type:" + entity.getContentType());
}
// the flag indicating whether the actual request has been fully transmitted to the connection target.
System.out.println("Sent flag: " + localContext.getAttribute(ExecutionContext.HTTP_REQ_SENT));
// entity , , 。
entity = response.getEntity();
EntityUtils.consume(entity);
}
출력:
Socket timeout: 0 Final target: http://www.baidu.com HTTP version: HTTP/1.1 HTTP Headers: Host: www.baidu.com Connection: Keep-Alive User-Agent: Apache-HttpClient/4.2.5 (java 1.5) HTTP URI: / Content Encoding:null Content Type:Content-Type: text/html;charset=utf-8 Sent flag: true
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 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에 따라 라이센스가 부여됩니다.