繁体   English   中英

通过套接字连接tomcat

[英]Connect tomcat through 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();
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM