Java httpmessage计时器
我正在尝试使用httpmessage和计时器。我正在读取一个URL,并在代码中插入一个横幅。我想运行一个计时器,如果用户已经在上面足够长的时间刷新页面和重新插入横幅。现在我有这样一个功能,用户只访问一次页面,在浏览其他网站时不再显示广告。它仅在刷新页面时重新插入横幅Java httpmessage计时器,java,Java,我正在尝试使用httpmessage和计时器。我正在读取一个URL,并在代码中插入一个横幅。我想运行一个计时器,如果用户已经在上面足够长的时间刷新页面和重新插入横幅。现在我有这样一个功能,用户只访问一次页面,在浏览其他网站时不再显示广告。它仅在刷新页面时重新插入横幅 import java.net.*; import java.io.*; import org.apache.commons.lang.StringEscapeUtils; import java.util.*; impo
import java.net.*;
import java.io.*;
import org.apache.commons.lang.StringEscapeUtils;
import java.util.*;
import java.nio.charset.*;
// public static int var = 0;
// ---------------
public static boolean runad = true;
public void main(HttpMessage httpMessage){
try{
//var++;
//Ignores if not html page
if (!runad) return;
runad = false;
String currentUrl = httpMessage.getUrl(); //Gets the url
//httpMessage.reply(currentUrl);
URL cURL = new URL(currentUrl); //creates a connection
URLConnection c = cURL.openConnection(); //connects to the url throws exception if access denied(e.g. futhead.com)
String str = c.getContentType(); //Gets type of content html files return text/html css return text/css etc.
if(str.indexOf("html") == -1) return; //if content type does not have html anywhere in it stop here
//Get currrent page html
URL url; //New URL
InputStream is = null; //New input stream
BufferedReader br; //New buffer reader
String line; //Temp string to hold each html line
String html = ""; //Final string to load all html code
url = new URL(httpMessage.getUrl()); //Gets the current URL
is = url.openStream(); // throws an IOException opens a new stream
br = new BufferedReader(new InputStreamReader(is)); //Opens new buffer reader with new stream
while ((line = br.readLine()) != null) { //reads each line in html and adds it to the ln string
html = html + StringEscapeUtils.escapeHtml(line) + "\n";
}
html = StringEscapeUtils.unescapeHtml(html);
/*************************Head link injection*******/
StringBuilder bodyInsert = new StringBuilder();
String inputLine;
URL advert = new URL("advertisingurl");
URLConnection ac = advert.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
ac.getInputStream(), "UTF-8"));
while ((inputLine = in.readLine()) != null)
bodyInsert.append(inputLine+"\n");
in.close();
String styleImport = bodyInsert.toString();
String jsImport = "";
String jQueryImport = "";
String bodyImport = "";
String head; //The beginnnig of the file to right before the </head> tag
String body; //</head> to right before the </body> tag
String bEND; //</body> tag to the end of the file
int hIndex = html.indexOf("</head"); //Index of </head
int bIndex = html.indexOf("</body"); //Index of </body
head = html.substring(0, hIndex);
body = html.substring(hIndex, bIndex);
bEND = html.substring(bIndex);
body = body + bodyImport;
head = head + styleImport + jsImport + jQueryImport;
String newHTML = StringEscapeUtils.escapeHtml(head + body + bEND);
newHTML = StringEscapeUtils.unescapeHtml(newHTML);
/*************************Head link injection*******/
httpMessage.reply(head + body + bEND);
Test test = new Test();
test.scheduleAtFixedRate(this, 0, 2000);
}catch(Exception e)
{
report(10, "Error in test Script", e.getMessage());
}
httpMessage.abortProcessing();
}
public class Test extends TimerTask {
private int age;
public Test() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(this, 0, 2000);
}
/**
* Implements TimerTask's abstract run method.
*/
public void run(){
//toy implementation
runad = true;
}
public void main(String[] args) {
new Test();
}
}
import java.net.*;
导入java.io.*;
导入org.apache.commons.lang.StringEscapeUtils;
导入java.util.*;
导入java.nio.charset.*;
//公共静态int var=0;
// ---------------
公共静态布尔runad=true;
public void main(HttpMessage HttpMessage){
试一试{
//var++;
//如果不是html页面,则忽略
如果(!runad)返回;
runad=false;
字符串currentUrl=httpMessage.getUrl();//获取url
//httpMessage.reply(当前URL);
URL cURL=newURL(currentUrl);//创建连接
URLConnection c=cURL.openConnection();//如果访问被拒绝,连接到url会引发异常(例如futhead.com)
String str=c.getContentType();//获取内容类型html文件返回文本/html css返回文本/css等。
if(str.indexOf(“html”)==-1)返回;//如果内容类型中没有html,请在此处停止
//获取当前页面html
URL;//新URL
InputStream=null;//新的输入流
BufferedReader br;//新的缓冲区读取器
String line;//用于保存每个html行的临时字符串
字符串html=”“;//加载所有html代码的最终字符串
url=新url(httpMessage.getUrl());//获取当前url
is=url.openStream();//引发IOException打开新流
br=new BufferedReader(new InputStreamReader(is));//使用新流打开新缓冲区读取器
而((line=br.readLine())!=null){//读取html中的每一行并将其添加到ln字符串中
html=html+StringEscapeUtils.escapeHtml(行)+“\n”;
}
html=StringEscapeUtils.unescapethtml(html);
/*************************头连杆注射*******/
StringBuilder bodyInsert=新StringBuilder();
字符串输入线;
URL广告=新的URL(“广告语”);
URLConnection ac=advert.openConnection();
BufferedReader in=新的BufferedReader(新的InputStreamReader(
ac.getInputStream(),“UTF-8”);
而((inputLine=in.readLine())!=null)
bodyInsert.append(inputLine+“\n”);
in.close();
字符串styleImport=bodyInsert.toString();
字符串jsImport=“”;
字符串jQueryImport=“”;
字符串bodyImport=“”;
String head;//标记前面的文件开头
String body;//在标记前面的右边
String bEND;//标记到文件的末尾
int hIndex=html.indexOf(“你能粘贴你的代码吗?”)这是一个javascript代码,用于计算“在”下花费的时间。准确地测量在每个页面上花费的时间“此链接的标题:不确定。但可能对您有帮助。这对我不起作用,因为我想让java计时,以便刷新页面以重新插入横幅。您可以粘贴代码吗?这是一个javascript代码,用于计算“准确测量每个页面上花费的时间”下的时间。”此链接的标题:不确定。但可能对您有所帮助。这对我来说不起作用,因为我希望java对其计时,以便刷新页面以重新插入横幅广告