如何通过java从服务器读取php echo而不需要javascript需要的页面?

时间:2016-03-22 20:48:56

标签: java php bufferedreader readfile inputstreamreader

我有一个java应用程序,它通过读取php脚本的echo来从服务器检索数据。

我正在使用此代码:

String url = new UrlBuilder(URL)
            .addParameter("option", "read")
            .toString();
BufferedReader br = null;
try { br = new BufferedReader(new InputStreamReader(new URL(url).openStream(), "UTF-8")); } 
catch (IOException e) { e.printStackTrace(); }
String out = "", line;
try {
     while ((line=br.readLine()) != null) {
         out += line;
    }
} catch (IOException e) { e.printStackTrace(); }
try {
    br.close();
} catch (IOException e) {
    e.printStackTrace();
}
return out;

UrlBuilder只是编码我的网址

我收到的回复是一个自动创建的网站,上面写着我需要激活javascript才能访问该网站:

<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("261516e5343951a035c7cc4ad11521ef");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; document.cookie="referrer="+escape(document.referrer); location.href="[servers url here]";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

那么我如何阅读文件的实际内容/来源? 或者我如何阻止服务器生成文件?

1.Attempt:提取cookie值:

try {
    URL myUrl = new URL(URL+"?option=read");
    URLConnection urlConn = myUrl.openConnection();
    urlConn.connect();
    String headerName=null;
    for (int i=1; (headerName = urlConn.getHeaderFieldKey(i))!=null; i++) {
        if (headerName.equals("__test")) {           
            String cookie = urlConn.getHeaderField(i);   
            cookie = cookie.substring(0, cookie.indexOf(";"));
            String cookieName = cookie.substring(0, cookie.indexOf("="));
            String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
            System.out.println(cookieName);
            System.out.println(cookieValue);
        }
     }
} catch(Exception e) {
    e.printStackTrace();
}

输出=无...

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

  1. 您可以add the http cookie使用名称&#39; __ test&#39;根据您的要求。可以使用浏览器工具找到JS计算的cookie值(F12)。可能是推荐人&#39;还需要cookie。例如:urlConn.setRequestProperty("Cookie", "__test=<cookie from browser>; referrer=google.com/search")在致电urlConn.connect()

  2. 之前
  3. 另一个选项是使用htmlUnit在服务器端运行页面javascript,并在所有脚本运行后获取dom。