首页 文章

登录https网站并仅使用核心Java API下载页面

提问于
浏览
0

我想使用用户名和密码登录到https网站,转到该网站的一个网址,然后在网址下载页面(也许可以解析该网页的内容) . 我想使用核心Java apis而不是htmlunit,jsoup等来做这个 . 我得到了下面的代码来学习如何做到这一点,但它没有告诉我如何登录到网站 . 请告诉我如何登录,维护会话,然后最终关闭连接 .

来源 - http://www.mkyong.com/java/java-https-client-httpsurlconnection-example/

import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.Certificate;
import java.io.*;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;

public class HttpsClient{

   public static void main(String[] args)
   {
        new HttpsClient().testIt();
   }

   private void testIt(){

      String https_url = "https://www.google.com/";
      URL url;
      try {

         url = new URL(https_url);
         HttpsURLConnection con = (HttpsURLConnection)url.openConnection();

         //dumpl all cert info
         print_https_cert(con);

         //dump all the content
         print_content(con);

      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }

   }

   private void print_https_cert(HttpsURLConnection con){

    if(con!=null){

      try {

    System.out.println("Response Code : " + con.getResponseCode());
    System.out.println("Cipher Suite : " + con.getCipherSuite());
    System.out.println("\n");

    Certificate[] certs = con.getServerCertificates();
    for(Certificate cert : certs){
       System.out.println("Cert Type : " + cert.getType());
       System.out.println("Cert Hash Code : " + cert.hashCode());
       System.out.println("Cert Public Key Algorithm : " 
                                    + cert.getPublicKey().getAlgorithm());
       System.out.println("Cert Public Key Format : " 
                                    + cert.getPublicKey().getFormat());
       System.out.println("\n");
    }

    } catch (SSLPeerUnverifiedException e) {
        e.printStackTrace();
    } catch (IOException e){
        e.printStackTrace();
    }

     }

   }

   private void print_content(HttpsURLConnection con){
    if(con!=null){

    try {

       System.out.println("****** Content of the URL ********");            
       BufferedReader br = 
        new BufferedReader(
            new InputStreamReader(con.getInputStream()));

       String input;

       while ((input = br.readLine()) != null){
          System.out.println(input);
       }
       br.close();

    } catch (IOException e) {
       e.printStackTrace();
    }

       }

   }

}

1 回答

  • 1

    每个网站以不同方式管理登录 . 您将需要侦察网站,了解会话的维护方式,并模拟功能,使服务器无法判断它不是浏览器 .

    通常,Web服务器在cookie中存储秘密哈希 . 这是过程

    • 使用HttpsURLConnection将登录名和密码发布到所述网址以发送表单 .

    • 服务器使用它希望存储在cookie中的标头中的哈希进行响应 . 通常在名称中有会话 .

    • 使用正确值中的标头中的散列发回请求

    以上所有操作只能使用URL和HttpsURLConnection完成,但您需要模仿浏览器才能欺骗服务器 .

    对于侦察,我建议使用像fiddler这样的工具 . 它捕获来自Web服务器和返回的所有通信,以便您可以准确地看到http级别上发生的事情,以模仿您的Java代码 .

    Here is an overview of fiddler . 我从来没有看过日志 . Fiddler有一个甜蜜的界面 . 视频真的很无聊,但它概述了界面 . 您想查看原始文本视图,并模仿它 .

    对于您的其他问题,owasp是最佳实践的绝佳资源 . 现实情况是,有很多不安全和不良的代码可以做你永远不会想到的东西 . 我已经看到服务器将boolean值放在脚本标记内,以存储为javascript变量 . 您只需要在登录后仔细观察服务器如何更改响应 . 对于遵循最佳实践的热门网站,他们将使用上述方法 .

相关问题