doupi4649 2015-05-12 16:56
浏览 151

用Java检查网页的X-Frame-Options

I already did something like this in PHP, but I need it in Java. This is my actual PHP code:

$error = false;
$ch = curl_init();

$options = array(
  CURLOPT_URL            => "http://google.com/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HEADER         => true,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_ENCODING       => "",
  CURLOPT_AUTOREFERER    => true,
  CURLOPT_CONNECTTIMEOUT => 120,
  CURLOPT_TIMEOUT        => 120,
  CURLOPT_MAXREDIRS      => 10,
);

curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch);
$headers = substr($response, 0, $httpCode['header_size']);

if(stripos($headers, 'X-Frame-Options: DENY') > -1 || 
   stripos($headers, 'X-Frame-Options: SAMEORIGIN')) {
  $error = true;
}

curl_close($ch);

So $error is false, but when webpage has set to disable embedding in iframe, it will set to true, so I know, that this webpage is not embeddable.

All I have in Java is this:

HttpURLConnection con = (HttpURLConnection) new URL("http://google.com/").openConnection();

What I have to do in Java to get same results as in PHP?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
    • ¥15 unity连接Sqlserver
    • ¥15 图中这种约束条件lingo该怎么表示出来
    • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
    • ¥15 流式socket文件传输答疑
    • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
    • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
    • ¥15 win10,这种情况怎么办
    • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
    • ¥100 在连接内网VPN时,如何同时保持互联网连接