1:HttpClient的get方式(程序中使用的HttpClient为4.3.1版本)
public static String doGet(String src,String unicode){
CloseableHttpResponse response = null;
HttpEntity entity = null;
InputStream is = null;
HttpGet get = new HttpGet(src);
CloseableHttpClient httpclient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();//设置请求和传输超时时间
get.setConfig(requestConfig);
String result = null;
try {
response = httpclient.execute(get);
if (null != response && response.getStatusLine().getStatusCode() == 200) {
entity = response.getEntity();
//得到流对象
is = entity.getContent();
System.out.println(is.toString());
//得到文档对象
result = EntityUtils.toString(entity, unicode);
System.out.println(result);
}
if (null != response && (is ==null)) {
logger.error("sendInfoForGET response err:" + response.getStatusLine() + ",url="+src);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String doPost(String src,Map<String,String> params,String unicode){
CloseableHttpResponse response = null;
HttpEntity entity = null;
HttpPost post = new HttpPost(src);
BasicCookieStore cookies = new BasicCookieStore();
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookies).build();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();//设置请求和传输超时时间
post.setConfig(requestConfig);
// 创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
//注入参数
for(String key:params.keySet()){
formparams.add(new BasicNameValuePair(key,params.get(key)));
}
UrlEncodedFormEntity uefEntity;
String result = null;;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
post.setEntity(uefEntity);
response = httpclient.execute(post);
if (null != response && response.getStatusLine().getStatusCode() == 200) {
entity = response.getEntity();
List<org.apache.http.cookie.Cookie> list = cookies.getCookies();
for (int i = 0; i < list.size(); i++) {
System.out.println("-"+list.get(i).toString());
}
}
if (null != response ) {
logger.error("sendInfoForGET response err:" + response.getStatusLine() + ",url="+src);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
3.使用java.net.url获取资源(一般用来获取图片资源,编码没有处理正确)
public static void urlConnect(String src){
URL url = null;
try {
url = new URL(src);
URLConnection con = url.openConnection();
con.setConnectTimeout(5*1000);
con.setReadTimeout(5*1000);
//获取流对象
InputStream is = con.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bi = new BufferedReader(isr);
String tempLine = null;
StringBuffer sb = new StringBuffer();
while((tempLine=bi.readLine())!=null){
tempLine = new String(tempLine.getBytes(),"gb2312");
sb.append(tempLine+"\n");
}
System.out.println(sb.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
4.使用jsoup获取文档
public static void connectByJsoup(String url){
Connection conn = Jsoup.connect(url);
//忽略文档类型
conn.ignoreContentType(true);
//设置请求来源,针对防盗链
conn.referrer("www.baidu.com");
//增加cookie
try {
Document doc = conn.cookies(new HashMap<>()).get();
System.out.println(doc.html());
} catch (IOException e) {
e.printStackTrace();
}
}