java:HTTP服务器和客户端之间的HTTP会话

时间:2016-07-27 08:28:07

标签: java http httpserver httpsession

我正在使用com.sun.net.HttpServer类来构建一个带有java的http服务器,如下所示:

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class SimpleHttpServer {

  public static void main(String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(8989), 0);
    server.createContext("/", new MyHandler());
    server.setExecutor(null); // creates a default executor
    server.start();
  }

  static class MyHandler implements HttpHandler {
    public void handle(HttpExchange t) throws IOException {

        //Read the request
        InputStream in = httpExchange.getRequestBody();
        StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer);
        String inputString = writer.toString();

        //prepare the response
        httpExchange.sendResponseHeaders(200, "Hi my faithful client".length());
        OutputStream os = httpExchange.getResponseBody();
        os.write("Hi my faithful client".getBytes());
        os.close();
    }
  }
}

我正在使用此客户端与此服务器通信:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static void main(String argv[]) throws IOException{
        String urlstr = "http://127.0.0.1:8989";
        URL url = new URL(urlstr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);

        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write("Hello HTTP server!! I'm your client1");

        InputStream in  = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder result = new StringBuilder();
        String line;
        while((line = reader.readLine()) != null) {
            result.append(line);
            System.out.println(result.toString());
        }
    }
}

它很有效。 但我现在想要的是服务器允许客户端与他进行通信,这意味着一系列请求/响应不仅仅是一个。所以这将是一个请求/响应循环。为此,我尝试通过向客户端添加这两行来向客户端添加一个请求作为第一步:

writer.flush();
writer.write("Hello HTTP server!! I'm your client2");

但它不起作用。只是第一个请求被服务器捕获。

如何更改代码以实现我的目的?

1 个答案:

答案 0 :(得分:1)

请检查以下代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test1 {

static String urlstr = "http://127.0.0.1:8989";
public static void main(String argv) throws IOException{
    URL url = new URL(urlstr);
    Test1 t = new Test1();
    for (int i = 0; i < 10; i++) {
        t.sendRequest("Hello HTTP server!! I'm your client" + i, url);
    }

}

private void sendRequest(String strToSend, URL url) throws IOException{

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);

    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    writer.write(strToSend);

    InputStream in  = connection.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder result = new StringBuilder();
    String line;
    while((line = reader.readLine()) != null) {
        result.append(line);
        System.out.println(result.toString());
    }
}

}