package Httpclient_test.Httpclient_test;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class HttpClient {
/**
* GET 方式连接
* url:链接
* headers 请求头部参数
* charset 编码
* return response
*/
public static String get(String url,Map<String ,String>headers,String charset) {
try {
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("GET");
for (String header : headers.keySet()) {
conn.setRequestProperty(header, headers.get(header));
}
conn.connect();
if(conn.getResponseCode()==404) {
return null;
}
BufferedReader reader=null;
if(conn.getHeaderField("Content-Encoding")!=null && conn.getHeaderField("Content-Encoding").equals("gzip")) {
reader=new BufferedReader(new InputStreamReader(new GZIPInputStream(conn.getInputStream()) , charset));
}else {
reader=new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
}
String line=null;
StringBuilder sb=new StringBuilder();
while((line=reader.readLine())!=null) {
sb.append(line);
}
conn.disconnect();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* GET 方式获取cookie
* url 链接
* headers 请求头部参数
* charset 编码
* return cookie
*/
public static String get_cookie(String url,Map<String ,String>headers,String charset) {
try {
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("GET");
for (String header : headers.keySet()) {
conn.setRequestProperty(header, headers.get(header));
}
conn.connect();
String cookie="";
Map<String,List<String>> respone_headers=conn.getHeaderFields();
for (String key : respone_headers.keySet()) {
if(key!=null &&key.equals("Set-Cookie")) {
System.err.println(respone_headers.get(key));
List<String> string = respone_headers.get(key);
StringBuilder builder = new StringBuilder();
// for (String str : string) {
// builder.append(str).toString();
// }
// cookie=builder.toString();
for (String string2 : string) {
if(string2.startsWith("JSESSIONID=")) {
cookie=string2;
}
}
}
}
conn.disconnect();
return cookie;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* post 方式连接
* @param url 链接
* @param params 请求参数
* @param headers 请求头部参数
* @param charSet 编码
* @return response
*/
public static String post(String url,Map<String ,String>params,Map<String ,String>headers,String charSet) {
try {
URL url1=new URL(url);
HttpURLConnection conn=(HttpURLConnection) url1.openConnection();
//输入参数
conn.setDoOutput(true);
//不使用缓存
conn.setUseCaches(false);
conn.setConnectTimeout(60*2000);
for (String header : headers.keySet()) {
conn.setRequestProperty(header, headers.get(header));
}
conn.setRequestMethod("POST");
conn.connect();
if(params!=null && params.keySet().size()>0) {
StringBuilder sb=new StringBuilder();
for (String key : params.keySet()) {
sb.append(key+"="+URLEncoder.encode(params.get(key),charSet)+"&");
}
String param=sb.delete(sb.length()-1, sb.length()).toString();
//System.err.println(param);
DataOutputStream out=new DataOutputStream(conn.getOutputStream());
out.writeBytes(param);
out.flush();
out.close();
}
BufferedReader reader=null;
if(conn.getHeaderField("Content-Encoding")!=null && conn.getHeaderField("Content-Encoding").equals("gzip")) {
reader=new BufferedReader(new InputStreamReader(new GZIPInputStream(conn.getInputStream()) , charSet));
}else {
reader=new BufferedReader(new InputStreamReader(conn.getInputStream(), charSet));
}
String line=null;
StringBuilder sb=new StringBuilder();
while((line=reader.readLine())!=null) {
sb.append(line);
}
conn.disconnect();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* post方式连接获取cookie
* @param url 链接
* @param params 请求参数
* @param headers 请求头部参数
* @param charSet 编码
* @return cookie
*/
public static String post_cookie(String url,Map<String ,String>params,Map<String ,String>headers,String charSet) {
try {
URL url1=new URL(url);
HttpURLConnection conn=(HttpURLConnection) url1.openConnection();
conn.setRequestMethod("POST");
//输入参数
conn.setDoOutput(true);
//不使用缓存
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(false);
conn.setConnectTimeout(60*1000);
for (String header : headers.keySet()) {
conn.setRequestProperty(header, headers.get(header));
}
conn.connect();
if(params!=null && params.keySet().size()>0) {
StringBuilder sb=new StringBuilder();
for (String key : params.keySet()) {
sb.append(key+"="+URLEncoder.encode(params.get(key),charSet)+"&");
}
String param=sb.delete(sb.length()-1, sb.length()).toString();
DataOutputStream out=new DataOutputStream(conn.getOutputStream());
out.writeBytes(param);
out.flush();
out.close();
}
String cookie="";
Map<String,List<String>> respone_headers=conn.getHeaderFields();
for (String key : respone_headers.keySet()) {
//System.err.println(respone_headers.get(key));
if(key!=null &&key.equals("Set-Cookie")) {
List<String> string = respone_headers.get(key);
StringBuilder builder = new StringBuilder();
for (String str : string) {
System.err.println(str);
builder.append(str).toString();
}
cookie=builder.toString();
}
}
conn.disconnect();
return cookie;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}