/codes.js system = require('system') address = system.args[1];//獲得命令行第二個參數 接下來會用到 //console.log('Loading a web page'); var page = require('webpage').create(); var url = address; //console.log(url); page.open(url, function (status) { //Page is loaded! if (status !== 'success') { console.log('Unable to post!'); } else { //console.log(page.content); //var title = page.evaluate(function() { // return document.title;//示範下如何使用頁面的jsapi去操作頁面的 www.oicqzone.com // }); //console.log(title); //console.log(encodeURIComponent(page.content)); console.log(page.content); } phantom.exit(); });
import org.apache.commons.io.IOUtils;
import java.io.*;
/**
* Created with IntelliJ IDEA.
* User: lsz
* Date: 14-4-22
* Time: 下午1:17
* utils for http
*/
public class HttpUtils {
public static String getAjaxCotnent(String url) throws IOException {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("phantomjs.exe c:/phantomjs/codes.js "+url);//這裏我的codes.js是保存在c盤下面的phantomjs目錄
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sbf = new StringBuffer();
String tmp = "";
while((tmp = br.readLine())!=null){
sbf.append(tmp);
}
//System.out.println(sbf.toString());
return sbf.toString();
}
public static void main(String[] args) throws IOException {
getAjaxCotnent("http://www.plusweb.cn");
}
}
C#實現代碼using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO; using System.Net; using System.Threading; namespace phantomjs { class Program { static void Main(string[] args) { // String str = getAjaxCotnent("http://www.plusweb.cn");//測試使用 new Program().listHttp(); } public static String getAjaxCotnent(String url) { ProcessStartInfo start = new ProcessStartInfo(Environment.CurrentDirectory + "//phantomjs//phantomjs.exe");//設置運行的命令行文件問ping.exe文件,這個文件系統會自己找到 //如果是其它exe文件,則有可能需要指定詳細路徑,如運行winRar.exe start.Arguments = "phantomjs//code.js" + " " + url;//設置命令參數 StringBuilder sb = new StringBuilder(); start.CreateNoWindow = false;//不顯示dos命令行窗口 start.RedirectStandardOutput = true;// start.RedirectStandardInput = true;// start.UseShellExecute = false;//是否指定操作系統外殼進程啓動程序 Process p = Process.Start(start); string encoding = p.StandardOutput.CurrentEncoding.ToString(); StreamReader reader = p.StandardOutput;//截取輸出流 string line = reader.ReadLine();//每次讀取一行 sb.AppendLine(line); while (!reader.EndOfStream) { line = reader.ReadLine(); sb.AppendLine(line); } p.WaitForExit();//等待程序執行完退出進程 p.Close();//關閉進程 reader.Close();//關閉流 string strRet = System.Web.HttpUtility.UrlDecode(sb.ToString()); return strRet; } private void listHttp() { using (HttpListener listerner = new HttpListener()) { string urllister = System.Configuration.ConfigurationSettings.AppSettings["listurl"]; listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份驗證 Anonymous匿名訪問 //listerner.Prefixes.Add(urllister); listerner.Prefixes.Add("http://localhost/web/"); listerner.Start(); Console.WriteLine("WebServer Start Successed......."); while (true) { //等待請求連接 //沒有請求則GetContext處於阻塞狀態 HttpListenerContext ctx = listerner.GetContext(); ctx.Response.StatusCode = 200;//設置返回給客服端http狀態代碼 string url = ctx.Request.QueryString["url"]; //使用Writer輸出http響應代碼 using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream)) { writer.WriteLine(getAjaxCotnent(url)); writer.Close(); ctx.Response.Close(); } } listerner.Stop(); } } } }啓動器c#程序訪問以下地址:
http://localhost/web/?url=http%3A%2F%2Fwww.plusweb.cn
進行測試