|
Posted on 2013-07-27 11:53 IceWee 阅读(1429) 评论(0) 编辑 收藏 所属分类: Java
备用。 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.List;
import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils;
/** *//** * <p> * HTTP请求工具类 * </p> * * @author IceWee * @date 2012-6-16 * @version 1.0 */ public class HttpClientUtils { private static final String NEXT_LINE = "\r\n"; private static final String HEADER_USER_AGENT = "User-Agent"; // 浏览器信息 private static final String BROWSER_IE9 = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; // 模拟Win7 x64/IE9
/** *//** * <p> * 网站登录(Post方式) * </p> * * @param url 登录请求URL * @param proxyHost 网络代理主机 * @param proxyPort 网络代理端口 * @param nvps POST参数列表 * @return 用户会话信息 */ public static List<Cookie> loginPost(String url, String proxyHost, int proxyPort, List <NameValuePair> nvps) { List<Cookie> cookies = new ArrayList<Cookie>(); DefaultHttpClient httpClient = new DefaultHttpClient(); try { if (proxyHost != null && !"".equals(proxyHost)) { // 网络代理 HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } HttpPost httpPost = new HttpPost(url); httpPost.setHeader(HEADER_USER_AGENT, BROWSER_IE9); // 模拟浏览器 httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); httpClient.execute(httpPost); cookies = httpClient.getCookieStore().getCookies(); } catch (Exception e) { e.printStackTrace(); } finally { httpClient.getConnectionManager().shutdown(); } return cookies; } /** *//** * 网站登录(Get方式) * * @param url 登录URL * @param encode 网站页面编码 * @param proxyHost 网络代理主机 * @param proxyPort 网络代理端口 * @return Cookie */ public static List<Cookie> loginGet(String url, String encode, String proxyHost, int proxyPort) { List<Cookie> cookies = null; DefaultHttpClient httpClient = new DefaultHttpClient(); try { if (proxyHost != null && !"".equals(proxyHost)) { // 网络代理 HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } HttpGet httpGet = new HttpGet(url); httpGet.setHeader(HEADER_USER_AGENT, BROWSER_IE9); // 模拟浏览器 HttpResponse response = httpClient.execute(httpGet); boolean succeed = getLoginResult(response, encode); if (succeed) { cookies = httpClient.getCookieStore().getCookies(); } else { cookies = null; } } catch (Exception e) { e.printStackTrace(); cookies = null; } finally { httpClient.getConnectionManager().shutdown(); } return cookies; } /** *//** * <p> * 网站注销(Get方式) * </p> * * @param url 注销请求URL * @param proxyHost 网络代理主机 * @param proxyPort 网络代理端口 * @param cookies 用户会话信息 */ public static void logout(String url, String proxyHost, int proxyPort, List<Cookie> cookies) { DefaultHttpClient httpClient = new DefaultHttpClient(); try { sendCookie(httpClient, cookies); if (proxyHost != null && !"".equals(proxyHost)) { // 网络代理 HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } HttpGet httpGet = new HttpGet(url); httpGet.setHeader(HEADER_USER_AGENT, BROWSER_IE9); // 模拟浏览器 httpClient.execute(httpGet); } catch (Exception e) { e.printStackTrace(); } finally { httpClient.getConnectionManager().shutdown(); } } /** *//** * <p> * POST请求网址 * </p> * * @param url 请求URL * @param proxyHost 网络代理主机 * @param proxyPort 网络代理端口 * @param encode 网站编码 * @param nvps 请求参数列表 * @param cookies 用户会话信息 * @return 网站源码 */ public static String sendPostRequest(String url, String proxyHost, int proxyPort, String encode, List <NameValuePair> nvps, List<Cookie> cookies) { String responseText = ""; DefaultHttpClient httpClient = new DefaultHttpClient(); try { sendCookie(httpClient, cookies); // 发送Cookie if (proxyHost != null && !"".equals(proxyHost)) { // 网络代理 HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } HttpPost httpPost = new HttpPost(url); httpPost.setHeader(HEADER_USER_AGENT, BROWSER_IE9); // 模拟浏览器 httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); responseText = getResponse(entity, encode); // 获取返回页面源码 EntityUtils.consume(entity); } catch (Exception e) { e.printStackTrace(); } finally { httpClient.getConnectionManager().shutdown(); } return responseText; } /** *//** * <p> * 解析返回流生成文本 * </p> * * @param entity * @param encode * @return * @throws IOException */ private static String getResponse(HttpEntity entity, String encode) throws IOException { StringBuffer buffer = new StringBuffer(); InputStream in = entity.getContent(); Reader reader = new InputStreamReader(in, encode); BufferedReader bufferedReader = new BufferedReader(reader); String line; while ((line = bufferedReader.readLine()) != null) { line = new String(line.getBytes()); buffer.append(line).append(NEXT_LINE); } bufferedReader.close(); reader.close(); return buffer.toString(); } /** *//** * <p> * 发送Cookie * </p> * * @param httpClient * @param cookies */ private static void sendCookie(DefaultHttpClient httpClient, List<Cookie> cookies) { if (cookies != null) { for (Cookie cookie : cookies) { httpClient.getCookieStore().addCookie(cookie); } } } /** *//** * <p> * 解析出登录结果,北大法宝登录成功失败分别返回如下信息:<br> * 成功:p_LoginUser = "elsevier";dispResult('');<br> * 失败:p_AlertInfo = "对不起,您的账号在“法律法规”库在线人数已经超过最大允许范围!如有任何疑问请致电400-810-8266转117";dispResult(''); * </p> * * @param response * @param encode * @return * @throws IOException */ private static boolean getLoginResult(HttpResponse response, String encode) throws IOException { boolean result = false; HttpEntity entity = response.getEntity(); String responseText = getResponse(entity, encode); EntityUtils.consume(entity); if (responseText != null) { if (responseText.contains("p_LoginUser")) { result = true; } else if (responseText.contains("p_AlertInfo")) { result = false; } } else { result = false; } return result; } }
|