java - Connect tomcat through socket -
i have been trying communicate tomcat using socket. forming http message , writing outputstream. connection gets established not receiving response. when trying connect through telnet same message able response. please find code snippet below , point missing.
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 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(); }
for convenience attaching servlet...it's basic one. created testing purpose.
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(); }
you using writeutf() should using write(), why doing @ all? why aren't using httpurlconnection or http client of sort? don't reinvent wheel.
Comments
Post a Comment