简体   繁体   English

Java无法从URL获取请求的文件

[英]Java trouble getting the requested file from a URL

I'm currently working on a project for school where the goal of the project is to establish a SSL connection with Java and display the requested file in a web browser. 我目前正在为一个学校项目工作,该项目的目标是与Java建立SSL连接并在网络浏览器中显示请求的文件。 I'm currently getting hung up trying to capture the request message so that I can parse it to get the name of the requested file. 我目前正挂断电话,尝试捕获请求消息,以便我可以对其进行解析以获取所请求文件的名称。

The SSL certificate is self signed so both Chrome and Internet Explorer come up with a warning about the "suspicious" site. SSL证书是自签名的,因此Chrome和Internet Explorer都会出现有关“可疑”网站的警告。 However at that point my code has already tried capturing the request message and failed by encountering a NullPointerException. 但是在那时,我的代码已经尝试捕获请求消息,但是由于遇到NullPointerException而失败。

My code looks like this: 我的代码如下所示:

    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(new FileInputStream(new File(keystore)), keystorepass);

    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    kmf.init(ks, keypassword);

    SSLContext sslcontext = SSLContext.getInstance("TLSv1.2");
    sslcontext.init(kmf.getKeyManagers(), null, null);

    ServerSocketFactory ssf = sslcontext.getServerSocketFactory();
    SSLServerSocket server = (SSLServerSocket)ssf.createServerSocket(65000);

    Socket connectionSocket = null;
    BufferedReader in = null;
    String requestedFile = null;
    boolean getRequest = true;

    while(getRequest || requestedFile == null)
    {
        try
        {
            System.out.println("Web Server: Listening for connection from Web browser");
            connectionSocket = server.accept();
            System.out.println("Web Server: Connection Established");

            in = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

            requestedFile = in.readLine();
            System.out.println(requestedFile);
            getRequest = false;
        }
        catch(Exception e)
        {
            connectionSocket.close();
        }

        if(requestedFile != null)
            System.out.println(requestedFile);

        //Here is where the NullPointerException occurs
        requestedFile = requestedFile.substring(requestedFile.indexOf("GET") + 4,
                requestedFile.indexOf("HTTP"));

There was another project that I did using just an HTTP connection where the goal of the project was the exact same, the only difference between the two was using a SSL connection. 我做过另一个项目,它仅使用HTTP连接,该项目的目标是完全相同的,两者之间的唯一区别是使用SSL连接。

The HTTP connection request message was captured like this: HTTP连接请求消息是这样捕获的:

        in = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        out = new DataOutputStream(connectionSocket.getOutputStream());

        String requestedFile;

        requestedFile = in.readLine();
        while((in.readLine()).length() > 0);

        requestedFile = requestedFile.substring(requestedFile.indexOf("GET") + 4, requestedFile.indexOf("HTTP"));

I tried implementing the while loop into the HTTPS project but didn't have any success. 我尝试将while循环实现到HTTPS项目中,但没有成功。 My only thought is that maybe it's not getting the request because of Chrome and Internet Explorer's security not sending it through until I say that I want to proceed to the site, and that is throwing off my code. 我唯一的想法是,由于Chrome和Internet Explorer的安全性直到我说我要继续访问该网站之前才发送请求,因此可能无法接收该请求,这使我的代码无法正常运行。 (Simply a guess) (只是一个猜测)

I know this is a school project and this community doesn't like to solve homework for students. 我知道这是一个学校项目,这个社区不喜欢为学生解决作业。 So posting here is a last resort after scouring StackOverflow and a lot of Googling. 因此,在仔细检查StackOverflow和大量Googling之后,在此处发布是最后的选择。 If someone could point me in the right direction as to why I'm not able to capture the request, that would be much appreciated. 如果有人可以向我指出为什么我无法捕获请求的正确方向,将不胜感激。

to accept the certificates use javax.net.ssl.TrustManager. 要接受证书,请使用javax.net.ssl.TrustManager。

TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }
            }
        };

        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM