我试图使用httpmessage和计时器。我正在读取URL,在代码中插入横幅。我想运行一个计时器,如果用户已经足够长时间刷新页面并重新插入横幅。现在,我有一个用户访问该页面的位置,并且在他们浏览其他网站时没有再次显示该广告。如果刷新页面,它只会重新插入横幅。
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();
}
}