通过套接字连接tomcat

时间:2013-08-23 22:49:43

标签: java sockets http java-ee tomcat

我一直在尝试使用socket与tomcat进行通信。我正在形成HTTP消息并将其写入outputStream。连接成功建立但我没有收到任何响应。当我试图通过telnet连接相同的消息能够得到响应。请找到下面的代码段并指出我错过的内容。

                String text1 = text.getText();
                String text2 = text_1.getText();
                String address = combo.getText();
                System.out.println("Text1 =" + text1 + " Text2 = " + text2 + " Address = "+address);
            try{
                StringBuilder builder = new StringBuilder();
                Socket socket = new Socket("localhost", 8082);
                DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
                BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                //U need to post actual HTTP MESSAGE HERE
                StringBuilder requestBuilder = new StringBuilder();
                requestBuilder.append("GET").append(SPACE).append(FORWARD_SLASH).append("SayHello").append(FORWARD_SLASH).append("SayHello").append("?")
                .append("text1=").append(text1).append("&text2=").append(text2).append(NEWLINE)/*.append(SPACE).append("HTTP/1.1").append(NEWLINE)
                .append("accept").append(COLON).append(SPACE).append("text/plain;").append(SPACE).append("text/html").append(NEWLINE)
                .append("accept-language").append(COLON).append("en-US").append(NEWLINE)
                .append("connection").append(COLON).append("keep-alive").append(NEWLINE)
                .append("host").append(COLON).append("localhost").append(NEWLINE)
                .append("user-agent").append(COLON).append("MyUCBrowser").append(NEWLINE)
                .append("content-length").append(COLON).append("0").append(NEWLINE)
                .append("connection-type").append(COLON).append("text/plain").append(NEWLINE)*/;

                /*box.setMessage(requestBuilder.toString());
                box.open();*/
                System.out.println(requestBuilder.toString());

                outToServer.writeUTF(requestBuilder.toString());
                String tempResp;
                while ((tempResp = inFromServer.readLine()) != null) {
                    builder.append(tempResp);
                }
                System.out.println(builder.toString().length() > 0 ? builder.toString() : "Anup Kalane!!!");
                text_2.setText(builder.toString());
                socket.close();
                display.dispose();
            }
            catch (UnknownHostException ex) {
                // TODO Auto-generated catch block
                ex.printStackTrace();
            } catch (IOException ex) {
                // TODO Auto-generated catch block
                ex.printStackTrace();
            }

为了方便附加servlet ......这是非常基本的。我刚刚创建它用于测试目的。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String text1 = request.getParameter("text1") != null ? request.getParameter("text1") : "NULL";
        String text2 = request.getParameter("text2") != null ? request.getParameter("text2") : "NULL";
        System.out.println("Hello");
        PrintWriter writer = response.getWriter();
        writer.write("<HTML><BODY><H1>HELLO\nText1 = "+text1+"\nText2 = "+text2+"</H1></BODY></HTML>");
        writer.close();
    }

1 个答案:

答案 0 :(得分:0)

您正在使用writeUTF(),您应该使用write(),但为什么要这样做呢?你为什么不使用HttpURLConnection或某种类型的HTTP客户端?不要重新发明轮子。