Java中使用多線程、curl及代理IP類比post提交和get訪問,javacurl
Java中使用多線程、curl及代理IP類比post提交和get訪問
菜鳥,多線程好玩就寫著玩,大神可以路過指教,小弟在這受教,謝謝!
/** * @組件名:javaDemo * @包名:javaDemo * @檔案名稱:Jenny.java * @建立時間: 2014年8月1日 下午5:53:48 * @著作權資訊:Copyright © 2014 eelly Co.Ltd,小姨子著作權。 */package javaDemo;import java.io.BufferedReader;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.Writer;import java.net.URL;import java.net.URLConnection;import java.util.List;import java.util.Map;import java.util.Random;/** * 用於異常的拋出而定義,因為Exception不能拋出,只有子類才能 */class MyException extends Exception { private static final long serialVersionUID = -1485825968429580934L; public MyException() { super(); } public MyException(String msg) { super(msg); } public MyException(String msg, Throwable cause) { super(msg, cause); } public MyException(Throwable cause) { super(cause); }}/** * 一個小姨子需要的線程類 */class ShiMengRen implements Runnable { /** * 瀏覽量的連結 */ private String url = "http://www.shimengren.com/article/yj/views.jhtml"; /** * 點贊的連結 */ private String urlZan = "http://www.shimengren.com/article/yj/ups.jhtml"; /** * 遊記的ID */ private String param = "article_id=10844"; /** * 多線程共用的全域變數 */ // private volatile boolean flag = true; /** * 同時進入post提交的線程數 */ private static final int MAX_THREAD_COUNT = 25; /** * 同時進入post提交的線程數的計數器 */ private volatile static int threadNum = 0; /** * 計數 */ private static int num = 0; /** * 瀏覽次數 */ private static int view = 0; /** * 代理IP */ private String[] proxyArray = { "127.0.0.1:8787", "115.239.210.199:80", "149.255.255.242:80", "124.172.242.175:80", "117.59.217.243:80", "183.234.59.89:18186", "117.59.217.236:80", "183.224.1.56:80", "120.202.249.230:80", "125.46.100.198:9999", "112.65.19.122:8080", "202.96.172.234:808", "183.224.1.114:80", "183.224.1.113:80", "222.66.115.229:80" }; // public void setFlag(boolean flag) { // this.flag = flag; // } // // public boolean getFlag() { // return this.flag; // } // // public void stop() { // this.setFlag(false); // } // // public void start() { // this.setFlag(true); // } public void setNum(int num) { ShiMengRen.num = num; } public int getNum() { return ShiMengRen.num; } public void setThreadNum(int threadNum) { ShiMengRen.threadNum = threadNum; } public int getThreadNum() { return ShiMengRen.threadNum; } public int getView() { return ShiMengRen.view; } /** * @方法名:getProxy * @描述:隨機擷取代理IP * @建立人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> * @修改人: * @修改時間:2014年8月18日 上午9:23:36 * @return * @傳回值:String[] * @異常說明: */ public String[] getProxy() { String[] strlist = null; int len = proxyArray.length - 1; int num = 0; if (0 < len) { num = new Random().nextInt(len); } String proxy = this.proxyArray[num]; if (proxy != "") { strlist = proxy.split(":"); } return strlist; } /** * @方法名:sendGet * @描述:向指定URL發送GET方法的請求 * @建立人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> * @修改人: * @修改時間:2014年8月18日 上午9:26:27 * @param url * 發送請求的URL * @param param * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 * @return URL 所代表遠端資源的響應結果 * @傳回值:String * @異常說明: */ public String sendGet(String url, String param) { String result = ""; BufferedReader in = null; String[] proxyList = this.getProxy(); if (null != proxyList) { System.setProperty("http.proxySet", "true"); System.setProperty("http.proxyHost", proxyList[0]); System.setProperty("http.proxyPort", proxyList[1]); } try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // 開啟和URL之間的串連 URLConnection connection = realUrl.openConnection(); // 設定通用的請求屬性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立實際的串連 connection.connect(); // 擷取所有回應標頭欄位 Map<String, List<String>> map = connection.getHeaderFields(); // 遍曆所有的回應標頭欄位 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定義 BufferedReader輸入資料流來讀取URL的響應 in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("發送GET請求出現異常!" + e); e.printStackTrace(); } // 使用finally塊來關閉輸入資料流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } /** * @方法名:sendPost * @描述:向指定 URL 發送POST方法的請求 * @建立人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> * @修改人: * @修改時間:2014年8月18日 上午9:29:09 * @param url * 發送請求的 URL * @param param * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 * @return 所代表遠端資源的響應結果 * @throws Exception * @傳回值:String * @異常說明: */ public String sendPost(String url, String param) throws Exception { PrintWriter out = null; BufferedReader in = null; String result = ""; String[] proxyList = this.getProxy(); if (null != proxyList) { System.setProperty("http.proxySet", "true"); System.setProperty("http.proxyHost", proxyList[0]); System.setProperty("http.proxyPort", proxyList[1]); } try { URL realUrl = new URL(url); // 開啟和URL之間的串連 URLConnection conn = realUrl.openConnection(); // 設定通用的請求屬性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 發送POST請求必須設定如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // 擷取URLConnection對象對應的輸出資料流 out = new PrintWriter(conn.getOutputStream()); // 發送請求參數 out.print(param); // flush輸出資料流的緩衝 out.flush(); // 定義BufferedReader輸入資料流來讀取URL的響應 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { throw new MyException("發送 POST 請求出現異常!" + e); // System.out.println("發送 POST 請求出現異常!" + e); // e.printStackTrace(); } // 使用finally塊來關閉輸出資料流、輸入資料流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { throw new MyException("發送 POST 請求出現異常!" + ex); // ex.printStackTrace(); } } return result; } /** * @方法名:plusNum * @描述:匯流排程數的計數 * @建立人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> * @修改人: * @修改時間:2014年8月18日 上午9:29:53 * @傳回值:void * @異常說明: */ public void plusNum() { ShiMengRen.num++; } /** * @方法名:reductionNum * @描述:匯流排程數的計數 * @建立人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> * @修改人: * @修改時間:2014年8月18日 上午9:30:03 * @傳回值:void * @異常說明: */ public void reductionNum() { ShiMengRen.num--; ShiMengRen.num = ShiMengRen.num < 0 ? 0 : ShiMengRen.num; } /** * @方法名:plusThreadNum * @描述:並發線程數計數(其實也不算是並發),使用同步標識符,防止多線程的幹擾 * @建立人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> * @修改人: * @修改時間:2014年8月18日 上午9:30:22 * @傳回值:void * @異常說明: */ public synchronized void plusThreadNum() { ShiMengRen.threadNum++; } /** * @方法名:reductionThreadNum * @描述:並發線程數計數(其實也不算是並發),使用同步標識符,防止多線程的幹擾 * @建立人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> * @修改人: * @修改時間:2014年8月18日 上午9:30:28 * @傳回值:void * @異常說明: */ public synchronized void reductionThreadNum() { ShiMengRen.threadNum--; ShiMengRen.threadNum = ShiMengRen.threadNum < 0 ? 0 : ShiMengRen.threadNum; } /** * @方法名:plusView * @描述:瀏覽量計數,使用同步標識符,防止多線程的幹擾 * @建立人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> * @修改人: * @修改時間:2014年8月18日 上午9:30:33 * @傳回值:void * @異常說明: */ public synchronized void plusView() { ShiMengRen.view++; } /** * @方法名:runShiMengRen * @描述:調用sendPost的方法,以及各種計數 * @建立人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> * @修改人: * @修改時間:2014年8月18日 上午9:30:37 * @throws MyException * @throws InterruptedException * @傳回值:void * @異常說明: */ public void runShiMengRen() throws MyException, InterruptedException { this.plusNum(); try { Thread.sleep(new Random().nextInt(2000)); if (ShiMengRen.MAX_THREAD_COUNT > this.getThreadNum()) { this.plusThreadNum(); // 計數器自增 String result = sendPost(this.url, this.param); // 刷瀏覽數 sendPost(this.urlZan, this.param); // 刷點贊數 if (result.equals("1")) { this.plusView(); this.jennyLog("瀏覽次數:" + this.getView() + "\n"); } } } catch (Exception e) { Thread.sleep(2000); throw new MyException("網站出現異常,停止所有線程."); } finally { System.out.println("等待執行線程隊列:" + this.getNum() + ",當前非同步線程數量:" + this.getThreadNum() + ",瀏覽次數:" + this.getView()); this.reductionNum(); this.reductionThreadNum(); } } /** * @方法名:jennyLog * @描述:小姨子的日誌記錄,只為看瀏覽量的變化 * @建立人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> * @修改人: * @修改時間:2014年8月18日 上午9:30:52 * @param data * @throws IOException * @傳回值:void * @異常說明: */ public void jennyLog(String data) throws IOException { File file = new File("/home/beyond/tmp/Jenny.log.txt"); // File file = new File("E:\\Jenny.log.txt"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } Writer out = new FileWriter(file); out.write(data); out.close(); } /** * <p> * 主題:run * </p> * <p> * 描述:往死裡拚命的Post提交資料 * </p> * * @see java.lang.Runnable#run() */ @Override public void run() { for (;;) { try { this.runShiMengRen(); } catch (Exception e) { System.out.println("發送 POST 請求出現異常."); } } }}/** * @類名:Jenny * @描述:小姨子類 * @建立人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> * @修改人: * @修改時間:2014年8月1日 下午5:53:48 * @修改說明:<br/> * @版本資訊:V1.0.0<br/> */public class JennyThread { /** * @方法名:main * @描述:小姨子的主方法 * @建立人:<a href=mailto: 529901956@qq.com>小姨子的姐夫</a> * @修改人: * @修改時間:2014年8月1日 下午5:53:48 * @param args * @throws Exception * @傳回值:void * @異常說明: */ public static void main(String[] args) { Runnable t = new ShiMengRen(); for (int i = 0; i < 60; i++) { new Thread(t, "Thread-" + i).start(); } }}
php類比post提交擷取資料 採集用 簡單說下
給分吧~
用CURL
$url=‘http://網址/json.php’;
$curlPost="a=123&b=456";
$ch = curl_init();//初始化curl
curl_setopt($ch,CURLOPT_URL, $url);//提交到指定網頁
curl_setopt($ch, CURLOPT_HEADER, 0);//設定header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果為字串且輸出到螢幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//運行curl
curl_close($ch);
print_r($data);//輸出結果
curl 類比post抓取問題,幫忙看看
設定個來源