java服务器侦听http请求

java服务器侦听http请求,java,http,post,server,Java,Http,Post,Server,我有一个处理post和get请求的代码。但它应该在post请求到来时响应。我不想使用servlet,因为它需要tomcat或jetty,而且它变得更复杂。 我该如何知道收到了post请求 private void sendPost() throws Exception { String url = "https://selfsolve.apple.com/wcResults.do"; URL obj = new URL(url); HttpsURLConnection

我有一个处理post和get请求的代码。但它应该在post请求到来时响应。我不想使用servlet,因为它需要tomcat或jetty,而且它变得更复杂。 我该如何知道收到了post请求

 private void sendPost() throws Exception {

    String url = "https://selfsolve.apple.com/wcResults.do";
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

    String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + urlParameters);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //print result
    System.out.println(response.toString());

}

 public static void main(String[] args) throws Exception {

    while (true) {
        HttpURLConnectionExample http = new HttpURLConnectionExample();

        System.out.println("Testing 1 - Send Http GET request");
        http.sendGet();

        System.out.println("\nTesting 2 - Send Http POST request");
        http.sendPost();

    }
}

首先在FireFox中安装TamperData,或者在浏览器中安装其他开发插件。然后你可以检查实际的沟通,并收集知识。w3c.org对HTTP协议有广泛的参考。制作自己的服务器(ServerSocket,Socket)相对容易。不过嵌入式码头应该发展得更快。@JoopEggen:谢谢你的帮助。我如何从jetty开始?我看了一些网站,但似乎很难。。。你能给我看一些简单快速的教程吗?(我个人做maven项目,所以库依赖关系是自动处理的。)还有jetty。