关于HTTP Server,客户端接收服务器未返回的数据

时间:2016-10-04 08:35:39

标签: java webserver httpclient nio httpserver

这是服务器和客户端代码,请帮我看看哪里出错,可以将数据发布到服务器但是没有收到httpResponse,告诉我原因,谢谢!

imagcode:POST

public  void dopost(){
    HttpClient httpClient = new DefaultHttpClient();
    try {
        HttpPost request = new HttpPost("http://127.0.0.1:8088");


        StringEntity params =new StringEntity("{\"username\":\"De\",\"passwd\":\"REsfg7ufghfgh\",\"public\":\"ERTYU45646\"} ");
        request.addHeader("content-type", "application/json");
        request.addHeader("Accept","application/json");
        request.addHeader(new BasicHeader("Cookie","JSESSIONID=B6FF25530B16AB46CA77B08129FECFB3"));
        request.setEntity(params);
        HttpResponse httpResponse = httpClient.execute(request);
        clientread();

        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(httpEntity.getContent(),"UTF-8"), 8 * 1024);
                StringBuilder entityStringBuilder = new StringBuilder();
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    entityStringBuilder.append(line + "/n");
                }
                System.out.println(entityStringBuilder.toString());
                JSONObject resultJsonObject = new JSONObject(entityStringBuilder.toString());
                System.out.println(resultJsonObject.toString());
            }

        }

    }catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }

}

一些服务器代码:SERVER2

服务器代码是:

else if (key.isWritable()) {
                    SocketChannel channel = (SocketChannel) key.channel();

                    JSONObject res1 = new JSONObject();
                    res1.put("result","OK");
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    byte[] bytes = res1.toString().getBytes();
                    buffer.put(bytes);
                    buffer.flip();
                    channel.write(buffer);
                    channel.shutdownInput();
                    channel.close();
                }

0 个答案:

没有答案