apache httpclient-4.3.1.jar重定向问题!

开源oschina 发布于 2014/01/11 01:26
阅读 6K+
收藏 0

【华为云1024程序员节·向云而生】预约直播 抽14件华为电子产品礼包!>>>

apache httpclient-4.3.1.jar 该包我找了官方网站也没找到怎么防止网页重定向,我不要重定向!求大神

我写的方法

package www.qq.dao;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
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.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.util.EntityUtils;

import www.qq.model.Cookie;
import www.qq.model.HTTPParameter;

/**
 * 阿帕奇HttpConnt
 * @author fuce
 *
 */
public class ApacheHttpConnt {
	
	public static void main(String[] args) throws ClientProtocolException, IOException {
		getHTTPSPost(null);
	}
	
	
	/**
	 * 统一关闭
	 * @param in
	 * @param entity
	 * @param response
	 */
	public static void HTTPSPostColes(BufferedReader in,HttpEntity entity,CloseableHttpResponse response){
		try {
			if(in!=null){
				in.close();
			}
			if(entity!=null){
				EntityUtils.consume(entity);
			}
			if(response!=null){
				 response.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	public static Cookie getHTTPS(HTTPParameter httPParameter){
		System.out.println();
		Cookie cookie=new Cookie();
		if ("POST".equals(httPParameter.getPisG())) {// 判断是get还是post
			cookie=getHTTPSPost(httPParameter);
		}else{
			cookie=getHTTPSGet(httPParameter);
		}
		return cookie;
	}
	
	/**
	 * 判断是txt还是img
	 * @param httpParameter
	 * @param inpt
	 */
	public static Cookie isTxtorImg(HTTPParameter httpParameter,InputStream inpt) {
		Cookie cookie=new Cookie();
		try {
			if("IMG".equals(httpParameter.getRetCodeType())){
				  //取图片数据
					if(inpt!=null){
						DataInputStream in = new DataInputStream(inpt);
						DataOutputStream out = new DataOutputStream(new FileOutputStream("d://验证码.jpg"));
						//将参数savePath,即将截取的图片的存储在本地地址赋
						byte[] buffer = new byte[1024];
						int count = 0;
						//将输入流以字节的形式读取并写入buffer
						while ((count = in.read(buffer)) > 0) {
							out.write(buffer, 0, count);
						}
						// 关闭输入输出流
						out.close();
						in.close();
					}
				}
				if("TXT".equals(httpParameter.getRetCodeType())){
					// 取数据
					if (inpt!=null) {
						// 读取返回的信息
						BufferedReader in = new BufferedReader(new InputStreamReader(inpt, "UTF-8"));
						StringBuffer line =new StringBuffer();
						String tem = "";
						while ((tem = in.readLine()) != null) {
							line.append(tem);
						}
						in.close();
						cookie.setCode(line.toString());
					}
				}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				inpt.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		return cookie;
			
			
	}
	
	
	/**
	 * httpsPost
	 * @param httPParameter
	 * @return
	 */
	public static Cookie getHTTPSPost(HTTPParameter httPParameter){
		Cookie cookie=new Cookie();
	    CloseableHttpClient httpclient=null;
	    HttpEntity entity=null;
	    BufferedReader in=null;
	    CloseableHttpResponse response=null;
	    HttpClientContext context = HttpClientContext.create();
	    try {
			if("HTTP".equals(httPParameter.getHttptype())) 
			{
				httpclient=HttpClients.createDefault();
			}else{
				httpclient = HttpClients.custom().setSSLSocketFactory(getSSLF()).build();
			}
			HttpPost httppost = new HttpPost(httPParameter.getUrl());
			
			StringEntity reqEntity2 = new StringEntity(httPParameter.getParameter(),"UTF-8");  
			 // 设置类型  
			reqEntity2.setContentType("application/x-www-form-urlencoded"); 
			
			httppost.setEntity(reqEntity2);
			
			if(httPParameter.getReferer()!=null){
				httppost.setHeader("Referer",httPParameter.getReferer());
			}
			
			if(httPParameter.getCookie()!=null){
				httppost.setHeader("Cookie",httPParameter.getCookie());
			}
			if(httPParameter.getHost()!=null){
				httppost.setHeader("Host",httPParameter.getHost());
			}
			httppost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0");
			
			response = httpclient.execute(httppost,context);
			entity = response.getEntity();
			System.out.println("执行状态:"+response.getStatusLine().getStatusCode());
			//获取文本
			InputStream	ins = entity.getContent();
			cookie=isTxtorImg(httPParameter, ins);
			Header[] headers=response.getHeaders("Set-Cookie");
			//获取cookie
			cookie.setCookie(getcookie(headers));
			
			//获取重定向值
			List<URI> list= context.getRedirectLocations();
			for (URI uri : list) {//重定向
				cookie.setLocation(uri.toURL().toString());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			HTTPSPostColes(in, entity, response);
		}
		return cookie;
		
		
	}
	
	/**
	 * httpsget
	 * @param httPParameter
	 * @return
	 */
	public static Cookie getHTTPSGet(HTTPParameter httPParameter){
		Cookie cookie=new Cookie();
	    CloseableHttpClient httpclient=null;
	    HttpEntity entity=null;
	    BufferedReader in=null;
	    CloseableHttpResponse response=null;
	    HttpClientContext context = HttpClientContext.create();
	   // LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
		try {
			if("HTTP".equals(httPParameter.getHttptype())) 
			{
				httpclient=HttpClients.createDefault();
			}else{
				httpclient = HttpClients.custom().setSSLSocketFactory(getSSLF()).build();
			}
			
			HttpGet  httpGet = new HttpGet(httPParameter.getUrl());
			if(httPParameter.getReferer()!=null){
				httpGet.setHeader("Referer",httPParameter.getReferer());
			}
			if(httPParameter.getCookie()!=null){
				httpGet.setHeader("Set-Cookie",httPParameter.getCookie());
			}
			if(httPParameter.getHost()!=null){
				httpGet.setHeader("Host",httPParameter.getHost());
			}
			httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0");
			response = httpclient.execute(httpGet,context);
			entity = response.getEntity();
//			System.out.println(response.getAllHeaders());
			System.out.println("执行状态:"+response.getStatusLine().getStatusCode());
			
			//获取文本
			InputStream	ins = entity.getContent();
			cookie=isTxtorImg(httPParameter, ins);
			Header[] headers=response.getHeaders("Set-Cookie");
			//获取cookie
			cookie.setCookie(getcookie(headers));
			System.out.println();
			//获取重定向值
			List<URI> list= context.getRedirectLocations();
			if(list!=null&&list.size()>0){
				for (URI uri : list) {//重定向
					cookie.setLocation(uri.toURL().toString());
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			HTTPSPostColes(in, entity, response);
		}
		return cookie;
		
		
	}
	
	public  static String getcookie(Header[] headers){
		StringBuffer buffer=new StringBuffer();
		for (Header header : headers) {
			buffer.append(header.getValue());
		}
		return buffer.toString();
	}
	
	/**
	 * 加载证书
	 * @return
	 * @throws Exception
	 */
	public static SSLConnectionSocketFactory getSSLF() throws Exception{
		SSLContextBuilder builder = new SSLContextBuilder();
	    builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
	    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
	    return sslsf;
	}
}


























java.net下面的 httpurlconnection方法

package www.qq.dao;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import www.qq.model.Cookie;
import www.qq.model.HTTPParameter;
/**
 * 模拟Post Get类
 * 
 * @author fuce
 * @time 2013-8-14
 */
public class HTTPConnt {
	/**
	 * SSL需要的
	 * 
	 * @author fuce
	 * @time 2013-8-14
	 */
	private static class TrustAnyTrustManager implements X509TrustManager {

		public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		public X509Certificate[] getAcceptedIssuers() {
			return new X509Certificate[] {};
		}
	}
	
	@SuppressWarnings("unused")
	private  class TrustAnyHostnameVerifier implements HostnameVerifier {
		public boolean verify(String hostname, SSLSession session) {
			return true;
		}
	}
	
	
	
	
	/**
	 * HTTP提交心跳
	 * @throws IOException 
	 */
	public static Cookie getHTTPPoll2(HTTPParameter httPParameter) throws Exception{
		Cookie cookie=new Cookie();
		URL url2 = new URL(httPParameter.getUrl());
		HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
		connection.setDoOutput(true);// 打开写入属性
		connection.setDoInput(true);// 打开读取属性
		connection.setReadTimeout(3000);
		//connection.setConnectTimeout(0);// 连接超时时间 设置连接主机超时(单位:毫秒)
		//connection.setReadTimeout(0);// 设置从主机读取数据超时(单位:毫秒
		connection.setInstanceFollowRedirects(false);// 设置是否重定向
		if(httPParameter.getReferer()!=null){
			connection.addRequestProperty("Referer", httPParameter.getReferer());
		}
		connection.disconnect();
		if(httPParameter.getCookie()!=null){
			connection.addRequestProperty("Cookie", httPParameter.getCookie());
		}
		if(httPParameter.getHost()!=null){
			connection.addRequestProperty("Host", httPParameter.getHost());
		}
		connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0");
		if ("POST".equals(httPParameter.getPisG())) {// 判断是get还是post
			
			connection.getOutputStream().write(httPParameter.getParameter().getBytes());
			connection.connect();// 提交
		} else {
			connection.setRequestMethod("GET");// 设置提交方式
			connection.connect();// 提交
		}
		
		
		if("IMG".equals(httPParameter.getRetCodeType())){
			  //取图片数据
				if(connection.getInputStream()!=null){
					DataInputStream in = new DataInputStream(connection.getInputStream());
					DataOutputStream out = new DataOutputStream(new FileOutputStream("d://验证码.jpg"));
					//将参数savePath,即将截取的图片的存储在本地地址赋
					byte[] buffer = new byte[1024];
					int count = 0;
					//将输入流以字节的形式读取并写入buffer
					while ((count = in.read(buffer)) > 0) {
						out.write(buffer, 0, count);
					}
					// 关闭输入输出流
					out.close();
					in.close();
				}
			}
			if("TXT".equals(httPParameter.getRetCodeType())){
				// 取数据
				if(connection.getInputStream()!=null){
				InputStream ins = connection.getInputStream();
				if (ins!=null) {
					// 读取返回的信息
					BufferedReader in = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
					StringBuffer line =new StringBuffer();
					String tem = "";
					while ((tem = in.readLine()) != null) {
						line.append(tem);
					}
					in.close();
					cookie.setCode(line.toString());
				}
				ins.close();
				
				}
			}
			
			// 取cookie
			if (connection.getHeaderField("Set-Cookie") != null) {
				String cookie1 = "";
				for (String s : connection.getHeaderFields().get("Set-Cookie")) {
					cookie1 += s;
				}
				cookie.setCookie(cookie1);
			}
			//重定向地址
			if (connection.getHeaderField("Location") != null) {
				String Location = "";
				for (String s : connection.getHeaderFields().get("Location")) {
					Location += s;
				}
				cookie.setLocation(Location);
			}
			
			connection.disconnect();// 关闭
			return cookie;
		
	}
	
	
	
	
	/**
	 * HTTP提交
	 * @throws IOException 
	 */
	public static Cookie getHTTPPost(HTTPParameter httPParameter) throws Exception{
		Cookie cookie=new Cookie();
		URL url2 = new URL(httPParameter.getUrl());
		HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
		connection.setDoOutput(true);// 打开写入属性
		connection.setDoInput(true);// 打开读取属性
		connection.setReadTimeout(3000);
		connection.setConnectTimeout(10000);// 连接超时时间 设置连接主机超时(单位:毫秒)
		connection.setReadTimeout(10000);// 设置从主机读取数据超时(单位:毫秒
		connection.setInstanceFollowRedirects(false);// 设置是否重定向
		if(httPParameter.getReferer()!=null){
			connection.addRequestProperty("Referer", httPParameter.getReferer());
		}
		
		if(httPParameter.getCookie()!=null){
			connection.addRequestProperty("Cookie", httPParameter.getCookie());
		}
		if(httPParameter.getHost()!=null){
			connection.addRequestProperty("Host", httPParameter.getHost());
		}
		connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0");
		if (httPParameter.getConnection() != null) {
			connection.addRequestProperty("Connection", httPParameter.getConnection());
		}
		
		if ("POST".equals(httPParameter.getPisG())) {// 判断是get还是post
			
			connection.getOutputStream().write(httPParameter.getParameter().getBytes());
			connection.connect();// 提交
		} else {
			connection.setRequestMethod("GET");// 设置提交方式
			connection.connect();// 提交
		}
		
		
		if("IMG".equals(httPParameter.getRetCodeType())){
			  //取图片数据
				if(connection.getInputStream()!=null){
					DataInputStream in = new DataInputStream(connection.getInputStream());
					DataOutputStream out = new DataOutputStream(new FileOutputStream("d://验证码.jpg"));
					//将参数savePath,即将截取的图片的存储在本地地址赋
					byte[] buffer = new byte[1024];
					int count = 0;
					//将输入流以字节的形式读取并写入buffer
					while ((count = in.read(buffer)) > 0) {
						out.write(buffer, 0, count);
					}
					// 关闭输入输出流
					out.close();
					in.close();
				}
			}
			if("TXT".equals(httPParameter.getRetCodeType())){
				// 取数据
				if(connection.getInputStream()!=null){
				InputStream ins = connection.getInputStream();
				if (ins!=null) {
					// 读取返回的信息
					BufferedReader in = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
					StringBuffer line =new StringBuffer();
					String tem = "";
					while ((tem = in.readLine()) != null) {
						line.append(tem);
					}
					in.close();
					cookie.setCode(line.toString());
				}
				ins.close();
				
				}
			}
			
			// 取cookie
			if (connection.getHeaderField("Set-Cookie") != null) {
				String cookie1 = "";
				for (String s : connection.getHeaderFields().get("Set-Cookie")) {
					cookie1 += s;
				}
				cookie.setCookie(cookie1);
			}
			//重定向地址
			if (connection.getHeaderField("Location") != null) {
				String Location = "";
				for (String s : connection.getHeaderFields().get("Location")) {
					Location += s;
				}
				cookie.setLocation(Location);
			}
			
			connection.disconnect();// 关闭
			return cookie;
		
	}
	/**
	 * HTTPS提交
	 * @throws MalformedURLException 
	 * @throws NoSuchAlgorithmException 
	 */
	public static Cookie getHTTPSPost(HTTPParameter httPParameter) throws Exception{
		Cookie cookie=new Cookie();
		URL url2 = new URL(httPParameter.getUrl());
		SSLContext sc = SSLContext.getInstance("SSL");
		sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
		HttpsURLConnection connection = (HttpsURLConnection) url2.openConnection();
		connection.setSSLSocketFactory(sc.getSocketFactory());
		connection.setDoOutput(true);// 打开写入属性
		connection.setDoInput(true);// 打开读取属性
		connection.setConnectTimeout(5000);// 连接超时时间 设置连接主机超时(单位:毫秒)
		connection.setReadTimeout(5000);// 设置从主机读取数据超时(单位:毫秒
		connection.setInstanceFollowRedirects(false);// 设置是否重定向
		if(httPParameter.getCookie()!=null){
			connection.addRequestProperty("Cookie", httPParameter.getCookie());
		}
		if(httPParameter.getHost()!=null){
			connection.addRequestProperty("Host", httPParameter.getHost());
		}
		connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63");
		if (httPParameter.getConnection() != null) {
			connection.addRequestProperty("Connection", httPParameter.getConnection());
		}
		
		if ("POST".equals(httPParameter.getPisG())) {// 判断是get还是post
			connection.getOutputStream().write(httPParameter.getParameter().getBytes());
			connection.connect();// 提交
		} else {
			connection.setRequestMethod("GET");// 设置提交方式
			connection.connect();// 提交
		}
		
		if("IMG".equals(httPParameter.getRetCodeType())){
			  //取图片数据
				if(connection.getInputStream()!=null){
					DataInputStream in = new DataInputStream(connection.getInputStream());
					DataOutputStream out = new DataOutputStream(new FileOutputStream("d://验证码.jpg"));
					//将参数savePath,即将截取的图片的存储在本地地址赋
					byte[] buffer = new byte[1024];
					int count = 0;
					//将输入流以字节的形式读取并写入buffer
					while ((count = in.read(buffer)) > 0) {
						out.write(buffer, 0, count);
					}
					// 关闭输入输出流
					out.close();
					in.close();
				}
			}
			if("TXT".equals(httPParameter.getRetCodeType())){
				// 取数据
				if(connection.getInputStream()!=null){
				InputStream ins = connection.getInputStream();
				if (ins!=null) {
					// 读取返回的信息
					BufferedReader in = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
					String line = "";
					String tem = "";
					while ((tem = in.readLine()) != null) {
						line += tem;
					}
					in.close();
					cookie.setCode(line);
				}
				ins.close();
				
				}
			}
			
			// 取cookie
			if (connection.getHeaderField("Set-Cookie") != null) {
				String cookie1 = "";
				for (String s : connection.getHeaderFields().get("Set-Cookie")) {
					cookie1 += s;
				}
				cookie.setCookie(cookie1);
			}
			//重定向地址
			if (connection.getHeaderField("Location") != null) {
				String Location = "";
				for (String s : connection.getHeaderFields().get("Location")) {
					Location += s;
				}
				cookie.setLocation(Location);
			}
			connection.disconnect();// 关闭
			return cookie;
	}
	

}



这个class只需要

connection.setInstanceFollowRedirects(false);// 设置是否重定向


就可以杜绝了重定向,


那个apache的怎么防止自动重定向- -求大神,

官网例子:

http://hc.apache.org/httpcomponents-client-4.3.x/index.html

api地址

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/overview-summary.html






加载中
0
南湖船老大
南湖船老大

新版本自动重定向了,你换个老版本吧。

刚看了下文档,http://hc.apache.org/httpcomponents-client-4.2.x/tutorial/html/httpagent.html#d5e1225

你试试ClientPNames.MAX_REDIRECTS='http.protocol.max-redirects' 这个参数。

0
德胜
德胜

官方文档没看完吧.....

LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
CloseableHttpClient httpclient = HttpClients.custom()
        .setRedirectStrategy(redirectStrategy)
        .build();
LaxRedirectStrategy重写一个类,继承这个,然后isRedirectable返回false就行了....没有实践过

不过httpclient真的是一个版本一个样,封装了下4.2结果换成4.3一片的过期方法....

0
K
Kevin_Do
HttpClient4.3.3中,httpparams类已经废弃了,如果要禁止重定向,新建httpclient实例时指定disableredirect即可,代码如下:

CloseableHttpClient httpclient = HttpClientBuilder.create().disableRedirectHandling().build();
返回顶部
顶部