简体   繁体   中英

My simple HTTPS Java server does not receive form's POST data

With much efforts, I finally was able to write a simple HTTPS server in Java to handle HTTPS requests. What I eventually should do is to get the data from the form, process it, and return the results back. Just as a start, in this simple example I just want to return the "value" of the submitted option. Unfortunately when I'm printing what the server has received, I don't see any POST data except POST /run.html HTTP/1.1 . What is the problem, and how can I add this feature into it?

PS: don't forget to create a keystore for the SSL and write the info in the code if you want to test.

The POST request I get is this (even the length is 41, which I guess should be correct for a missing data of list=capacity.3Mbps_400RTT_PER_0.0001.txt ):

POST /run.html HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: https://localhost:8888/
Accept-Language: en-US
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:8888
Content-Length: 41
Connection: Keep-Alive
Cache-Control: no-cache

Here is my simple form in HTML (which actually I'm supposed to receive its HTML source from server):

<html><body>
<p>
<h3> Please select a configuration below to run the emulator.</h3>
<form action="run.html" method="POST">
<select name="list">

  <option name="400" value="capacity.3Mbps_400RTT_PER_0.0001.txt">capacity.3Mbps_400RTT_PER_0.0001</option>
  <option name="100" value="capacity.3Mbps_100RTT_PER_0.00001.txt">capacity.3Mbps_100RTT_PER_0.00001</option>
  <option name="200" value="capacity.3Mbps_200RTT_PER_0.0001.txt">capacity.3Mbps_200RTT_PER_0.0001</option>

</select>
<input type="submit" value="Run Emulator!">
</form>

</body></html>

Here is my Java HTTPS server program:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;

public class HttpsServer {
    public static void main(String[] args) {
        String ksName = "myJKS.jks";
        char ksPass[] = "key".toCharArray();
        char ctPass[] = "key".toCharArray();
        try {

//          Runtime.getRuntime().exec("notepad");
            KeyStore ks = KeyStore.getInstance("JKS");
            ks.load(new FileInputStream(ksName), ksPass);
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            kmf.init(ks, ctPass);
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(kmf.getKeyManagers(), null, null);
            SSLServerSocketFactory ssf = sc.getServerSocketFactory();
            SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(8888,0, null);

            System.out.println("Server started:");
            printServerSocketInfo(s);
            // Listening to the port
            int count = 0;
            while (true) {
                SSLSocket c = (SSLSocket) s.accept();
                // Someone is calling this server
                count++;
                System.out.println("Connection #: " + count);
                // printSocketInfo(c);
                BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
                        c.getOutputStream()));
                BufferedReader r = new BufferedReader(new InputStreamReader(
                        c.getInputStream()));
                String m = r.readLine();
                System.out.println(m);
//              w.write(m);
                if (m != null) {
                    // We have a real data connection
                    w.write("HTTP/1.1 200 OK");
                    w.newLine();
                    w.write("Content-Type: text/html");
                    //should be two new lines!!!
                    w.newLine();
                    w.newLine();
                    if(m.contains("GET"))
                    //read from file
                    MyFileReader(w,"index.html");

                    while ((m = r.readLine()) != null) {
                        if (m.length() == 0)
                            break; // End of a GET call
//                      w.write(m);
                        System.out.println(m);
                        w.newLine();
                    }
                    w.flush();
                }
                w.close();
                r.close();
                c.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void MyFileReader(BufferedWriter w, String uri) {
        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader(uri));

            while ((sCurrentLine = br.readLine()) != null) {
                w.write(sCurrentLine);
                System.out.println(sCurrentLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    private static void printSocketInfo(SSLSocket s) {
        System.out.println("Server socket class: " + s.getClass());
        System.out.println("   Remote address = "
                + s.getInetAddress().toString());
        System.out.println("   Remote port = " + s.getPort());
        System.out.println("   Local socket address = "
                + s.getLocalSocketAddress().toString());
        System.out.println("   Local address = "
                + s.getLocalAddress().toString());
        System.out.println("   Local port = " + s.getLocalPort());
    }

    private static void printServerSocketInfo(SSLServerSocket s) {
        System.out.println("Server socket class: " + s.getClass());
        System.out.println("   Socker address = "
                + s.getInetAddress().toString());
        System.out.println("   Socker port = " + s.getLocalPort());
        System.out.println("   Need client authentication = "
                + s.getNeedClientAuth());
        System.out.println("   Want client authentication = "
                + s.getWantClientAuth());
        System.out.println("   Use client mode = " + s.getUseClientMode());
    }
}

How are you handling the POST method in your server? I see you have this portion for the GET method, looks like you need to add an else/if statement for POST method.

if(m.contains("GET")) {
  // return page HTML
} else if(m.contains("POST")) {
  // process form data
  // return page HTML
}

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