HttpClient的POST和GET請求和Session的保持-yellowcong
阿新 • • 發佈:2019-02-05
對於Http的get和post是無狀態的,所以我們需要將socket儲存,然後帶著socket去訪問網站,就好像我們瀏覽器去訪問一樣,就可以爬去登入後的資料
HttpClient工具類所用到的jar包
其實這個jar包,真是很淡騰,由於HttpClient比較的重要,所以和Java自帶的.net包中的有些重複,所以我就把這個jar貼出來了
commons-codec-1.9.jar
commons-logging-1.2.jar
fluent-hc-4.5.3.jar
httpclient-cache-4.5.3.jar
httpclient-win-4.5.3.jar
httpcore-4.4.6.jar
httpmime-4.5.3.jar
jna-4.1.0.jar
jna-platform-4.1.0.jar
httpclient-4.5.3.jar
log4j-1.2.17.jar
jsoup-1.7.3.jar
專案地址
https://gitee.com/yellowcong/utils
maven依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>yellowcong.com</groupId>
<artifactId>utils-httpclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging >
<name>utils-httpclient</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<httpclient.version>4.5.3</httpclient.version>
<log4j.version>1.2.17</log4j.version>
<commons-logging.version>1.2</commons-logging.version>
<jsoup.version>1.7.3</jsoup.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<!-- 配置日誌資訊 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons-logging.version}</version>
</dependency>
<!-- 網頁解析工具 -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency>
<!-- 測試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
工具類
傳送請求的時候,我們需要設定並配置編碼,不然,獲取到的是亂碼,就蛋疼了,說實話,我們可以通過獲取第一個介面,然後獲取html裡面的編碼,然後自動設定編碼。
package com.yellowcong.http.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HeaderIterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class HttpClient {
private static final Logger LOG = LogManager.getLogger(HttpClient.class);
/** 請求網站的編碼,這個地方,我預設 寫的是GB3212*/
private static final String DEFALUT_ENCODE = "GB2312";
public static CloseableHttpClient httpClient = null;
public static HttpClientContext context = null;
public static CookieStore cookieStore = null;
public static RequestConfig requestConfig = null;
static {
init();
}
private static void init() {
context = HttpClientContext.create();
cookieStore = new BasicCookieStore();
// 配置超時時間(連線服務端超時1秒,請求資料返回超時2秒)
requestConfig = RequestConfig.custom().setConnectTimeout(120000).setSocketTimeout(60000)
.setConnectionRequestTimeout(60000).build();
// 設定預設跳轉以及儲存cookie
httpClient = HttpClientBuilder.create().setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
.setRedirectStrategy(new DefaultRedirectStrategy()).setDefaultRequestConfig(requestConfig)
.setDefaultCookieStore(cookieStore).build();
}
/**
* 傳送get請求
*
* @param url
* @return response
* @throws ClientProtocolException
* @throws IOException
*/
public static String get(String url) {
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = null;
try {
//設定請求的引數
response= httpClient.execute(httpget, context);
return copyResponse2Str(response);
} catch(Exception e){
LOG.debug("請求失敗\t"+url);
}finally {
try {
if(response != null){
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 將返回的Response轉化成String物件
* @param response 返回的Response
* @return
*/
private static String copyResponse2Str(CloseableHttpResponse response){
try {
int code = response.getStatusLine().getStatusCode();
//當請求的code返回值不是400的情況
if((code == HttpStatus.SC_MOVED_TEMPORARILY )
|| (code == HttpStatus.SC_MOVED_PERMANENTLY)
|| (code == HttpStatus.SC_SEE_OTHER)
|| (code == HttpStatus.SC_TEMPORARY_REDIRECT)) {
return null;
}else{
return copyInputStream2Str(response.getEntity().getContent());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 將InputStream轉化為String型別的資料
* @param in
* @return
*/
private static String copyInputStream2Str(InputStream in){
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in,DEFALUT_ENCODE));
String line = null;
StringBuffer sb = new StringBuffer();
while((line = reader.readLine()) != null){
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
LOG.debug("獲取字串失敗");
}
return null;
}
/**
* 傳送post請求,不帶引數 的post
* @param url
* @return
*/
public static String post(String url){
return post(url, null);
}
/**
* 發從post 請求
* @param url
* @param parameters
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String post(String url, Map<String,Object> parameters){
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
try {
//設定請求的引數
setRequestParamter(parameters, httpPost);
//傳送請求
response = httpClient.execute(httpPost, context);
return copyResponse2Str(response);
}catch(Exception e){
LOG.debug("請求失敗\t"+url);
}finally {
try {
if(response != null){
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 設定POST請求的引數
* @param parameters
* @param httpPost
* @throws UnsupportedEncodingException
*/
private static void setRequestParamter(Map<String, Object> parameters, HttpPost httpPost)
throws UnsupportedEncodingException {
List<NameValuePair> nvps;
//新增引數
if(parameters != null && parameters.size()>0){
nvps = new ArrayList<NameValuePair>();
for(Map.Entry<String, Object> map:parameters.entrySet()){
NameValuePair param = new BasicNameValuePair(map.getKey(), map.getValue().toString());
nvps.add(param);
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, DEFALUT_ENCODE));
}
}
/**
* 將 http://www.yellowcong.com?age=7&name=8
* 這種age=7&name=8 轉化為map資料
* @param parameters
* @return
*/
@SuppressWarnings("unused")
private static List<NameValuePair> toNameValuePairList(String parameters) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
String[] paramList = parameters.split("&");
for (String parm : paramList) {
int index = -1;
for (int i = 0; i < parm.length(); i++) {
index = parm.indexOf("=");
break;
}
String key = parm.substring(0, index);
String value = parm.substring(++index, parm.length());
nvps.add(new BasicNameValuePair(key, value));
}
System.out.println(nvps.toString());
return nvps;
}
/**
* 手動增加cookie
* @param name
* @param value
* @param domain
* @param path
*/
public void addCookie(String name, String value, String domain, String path) {
BasicClientCookie cookie = new BasicClientCookie(name, value);
cookie.setDomain(domain);
cookie.setPath(path);
cookieStore.addCookie(cookie);
}
/**
* 把結果console出來
*
* @param httpResponse
* @throws ParseException
* @throws IOException
*/
public static void printResponse(HttpResponse httpResponse) throws ParseException, IOException {
// 獲取響應訊息實體
HttpEntity entity = httpResponse.getEntity();
// 響應狀態
System.out.println("status:" + httpResponse.getStatusLine());
System.out.println("headers:");
HeaderIterator iterator = httpResponse.headerIterator();
while (iterator.hasNext()) {
System.out.println("\t" + iterator.next());
}
}
/**
* 把當前cookie從控制檯輸出出來
*
*/
public static void printCookies() {
cookieStore = context.getCookieStore();
List<Cookie> cookies = cookieStore.getCookies();
for (Cookie cookie : cookies) {
System.out.println("key:" + cookie.getName() + " value:" + cookie.getValue());
}
}
/**
* 檢查cookie的鍵值是否包含傳參
*
* @param key
* @return
*/
public static boolean checkCookie(String key) {
cookieStore = context.getCookieStore();
List<Cookie> cookies = cookieStore.getCookies();
boolean res = false;
for (Cookie cookie : cookies) {
if (cookie.getName().equals(key)) {
res = true;
break;
}
}
return res;
}
/**
* 直接把Response內的Entity內容轉換成String
*
* @param httpResponse
* @return
* @throws ParseException
* @throws IOException
*/
public static String toString(CloseableHttpResponse httpResponse) throws ParseException, IOException {
// 獲取響應訊息實體
HttpEntity entity = httpResponse.getEntity();
if (entity != null)
return EntityUtils.toString(entity);
else
return null;
}
}