首先創建一下代理的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;
}
}