JAVA 动态替换代理IP并模拟POST请求

首先创建一下代理的IP和端口

package postTest;

import java.util.List;

/**
 * 
 * 获取代理信息
 * 
 * */
public class ProxyInfo {
	String proxyIP = null;
	String proxyPort = null;
	
    public String getProxyIP() {
		return proxyIP;
	}
	public void setProxyIP(String proxyIP) {
		this.proxyIP = proxyIP;
	}
	public String getProxyPort() {
		return proxyPort;
	}
	public void setProxyPort(String proxyPort) {
		this.proxyPort = proxyPort;
	}
	public static List<ProxyInfo> arrayList() {
		// TODO Auto-generated method stub
		
		return null;
	}
}
然后从文件中读入代理IP和端口列表,以空格分割

package postTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class ReadFile {
	/**
	 *	读取TXT文件中的代理配置文件
	 *	1、获得文件句柄
	 *	2、 获得的句柄当作一个字节码流,对输入的码流进行读取
	 *	读取到输入流之后,需要读取生成字节流
	 * 	逐行输出
	 * 	考虑到异常情况
	 * */
	public static ArrayList<ProxyInfo> readFile(String filePath){
		String encoding = "UTF-8";
		File file = new File(filePath);
		ProxyInfo proxys = new ProxyInfo();
		ArrayList<ProxyInfo> infos = new ArrayList<ProxyInfo>();
		if(file.isFile() && file.exists()){//判断文件是否存在
			try {
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file), encoding);//编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String textLine = null;
				
				for(int i = 0; (textLine = bufferedReader.readLine()) != null; i++){
					String[] proxyInfo = textLine.split(" ");
					proxys.setProxyIP(proxyInfo[0]);
					proxys.setProxyPort(proxyInfo[1]);
					infos.add(proxys);
					//System.out.println(((ProxyInfo) infos.get(i)).getProxyIP());
					//System.out.println(((ProxyInfo) infos.get(i)).getProxyPort());
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e){
				e.printStackTrace();
			}
		}else{
			System.out.println("Can not find the Proxy File");
		}
		return infos;
	}
	/*public static void main(String[] args){
		readFile("D:/proxy.txt");
	} */
}

然后,将读入的IP列表设置为代理

package postTest;

import postTest.*;

import org.apache.log4j.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class TestPost {  
	
	private static final Logger log = Logger.getLogger(TestPost.class);
	
    public static void TestPost() throws IOException {  
    	/*ProxyInfo proxyInfo = new ProxyInfo();
    	String proxyIP = proxyInfo.getProxyIP();
        int proxyPort = proxyInfo.getProxyPort();*/
    	
    	//ReadFile.readFile("");
    	
		ArrayList<ProxyInfo> abc = ReadFile.readFile("D:/proxy.txt");
        URL url = new URL("http://vote.sinabz.com/index.php?c=Index&a=tp");  
        for(int i = 0; i < 1; i++ ){
        	//Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress("127.0.0.1", 8087));
        	System.getProperty("http.maxRedirects", "50");
        	System.getProperties().setProperty("proxySet", "true");
        	System.out.println("此时的代理服务器设置为" + abc.get(i).getProxyIP()
        			+ "端口号设置为" + abc.get(i).getProxyPort());
        	URLConnection connection = url.openConnection(); 
        	connection.setRequestProperty(//设置终端类型
        			"User-Agent",
        			"Mozilla/4.0 (compatible; MSIE 7.0; NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)"
        			);  
        	System.getProperties().setProperty("http.proxyHost", abc.get(i).getProxyIP());
        	System.getProperties().setProperty("http.proxyPort", abc.get(i).getProxyPort());
        
            connection.setDoOutput(true);  
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");  
            out.write("id=19&submit="); // 向页面传递数据。post的关键所在!  
            out.flush();  
            out.close();  
            // 一旦发送成功,用以下方法就可以得到服务器的回应:  
            String sCurrentLine;  
            String sTotalString;  
            sCurrentLine = "";  
            sTotalString = "";  
            InputStream l_urlStream;  
            l_urlStream = connection.getInputStream();  
            // 传说中的三层包装阿!  
            BufferedReader l_reader = new BufferedReader(new InputStreamReader(  
                    l_urlStream));  
            while ((sCurrentLine = l_reader.readLine()) != null) {  
                sTotalString += sCurrentLine + "\r\n";  
      
            }  
            System.out.println(sTotalString);  
        	log.info(getHtml("http://www.ip138.com/ips138.asp?"));  
        }  
    }  
  
	public static void main(String[] args) throws IOException {  
    	//System.setProperties("");
    	//System.setProperty("", "");
        TestPost();  
    }  
	/****************下面是从IP138获取一下是否正确设置了代理服务器**************/
	private static String getHtml(String address){  
        StringBuffer html = new StringBuffer();  
        String result = null;  
        try{  
            URL url = new URL(address);  
            URLConnection conn = url.openConnection();  
            conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");  
            BufferedInputStream in = new BufferedInputStream(conn.getInputStream());  
              
            try{  
                String inputLine;  
                byte[] buf = new byte[4096];  
                int bytesRead = 0;  
                while (bytesRead >= 0) {  
                    inputLine = new String(buf, 0, bytesRead, "ISO-8859-1");  
                    html.append(inputLine);  
                    bytesRead = in.read(buf);  
                    inputLine = null;  
                }  
                buf = null;  
            }finally{  
                in.close();  
                conn = null;  
                url = null;  
            }  
            result = new String(html.toString().trim().getBytes("ISO-8859-1"), "gb2312").toLowerCase();  
              
        }catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }finally{  
            html = null;              
        }  
        System.out.println(result);
        return result;  
    }  
}  
  


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值