简体   繁体   中英

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. 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. However at that point my code has already tried capturing the request message and failed by encountering a 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.

The HTTP connection request message was captured like this:

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

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) {
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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