dsovc00684 2016-03-22 20:48
浏览 93

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

I got a java application which retrieves data from a server by reading the echo of an php script.

I am using this code:

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;

The UrlBuilder is just encoding my url

The reply I am getting is an automatically created site which says I need javascript activated to access the site:

<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>

So how I read the actual content/source of the file? Or how can I prevent the server from generating the file?

1.Attempt: Extract cookie value:

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();
}

Output=none...

Where is my mistake?

  • 写回答

1条回答 默认 最新

  • dprfe04886 2016-03-22 21:14
    关注
    1. You could add the http cookie with name '__test' to your request. JS calculated cookie value can be found with browser tools (F12). Probably 'referrer' cookie is also needed. For example: urlConn.setRequestProperty("Cookie", "__test=<cookie from browser>; referrer=google.com/search") before calling urlConn.connect()

    2. Another option can be using htmlUnit to run the page javascript on server side and fetch the dom after all scripts have run.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?