我用J2SE写了一个访问服务器的小程序,服务器端用servlet响应,客户端发消息给服务器端,服务器在收到消息后,将消息传回客户端,问题是服务器端没有收到发的消息
客户端程序:
import java.io.*;
import java.net.*;
public class Transmit_2{
public static void main(String[] args) {
URL url=null;
String xx=("http://localhost:8080/servlet/Receive");
OutputStream out=null;
InputStream in=null;
HttpURLConnection go=null;
String query="stone";
try{
System.out.println("Creating URL");
url=new URL(xx);
System.out.println("Opening Stream");
go=(HttpURLConnection)url.openConnection();
go.setRequestMethod("POST");
go.setDoOutput(true);
out=go.getOutputStream();
DataOutputStream buffer1=new DataOutputStream(out);
if(go.getResponseCode()==200){
System.out.println("OK");
}
System.out.println("Transmiting data");
buffer1.writeChars(query);
buffer1.close();
buffer1=null;
byte[]b=new byte[13];
in=go.getInputStream();
DataInputStream buffer2=new DataInputStream(in);
buffer2.readFully(b);
buffer2.close();
String lik=new String(b);
System.out.println(lik);
}
catch(MalformedURLException e){
System.out.println("Bad URL");
}
catch(IOException e){
System.out.println("IO Error"+e.getMessage());
}
}

服务器端程序:
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Receive extends HttpServlet {public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
InputStreamReader buffered=new InputStreamReader(req.getInputStream());
System.out.println("******"+buffered+"********");
BufferedReader nn=new BufferedReader(buffered);
System.out.println("******"+nn+"******");
String lisy=nn.readLine();
System.out.println("******"+nn.readLine()+"********");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
if (lisy!=null)
{
if(lisy.equals("stone")){
out.println(lisy+"good");
}
else
{
out.println(lisy+"Fail");
}
}
else
{
out.println("Nothing,NULL");
}
out.close();
}
public void destroy() {}
}
每次访问服务器返回的结果都是
Creating URL
Opening Stream
OK
Transmiting data
Nothing,NULL在服务器端得到输出
******java.io.InputStreamReader@15bdc50********
******java.io.BufferedReader@1dd3812******
******null********我在服务器端监听端口8080得到的是
报头content.length=0
说明根本就没有把信息发过去
我实在不知道该怎样传送消息,请各位高手帮帮忙,小弟不甚感激!

解决方案 »

  1.   

    用jms可以实现你要的结果
      

  2.   

    既然你使用http协议,你就应该生成http包头才可以。
      

  3.   

    你得按http来post数据,
    可以用URLConnection:
    try {
            // Construct data
            String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
            data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
        
            // Send data
            URL url = new URL("http://hostname:80/cgi");
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();
        
            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                // Process line...
            }
            wr.close();
            rd.close();
        } catch (Exception e) {
        }当然也可以用socket这样的话得写头等:
    try {
            // Construct data
            String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
            data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
        
            // Create a socket to the host
            String hostname = "hostname.com";
            int port = 80;
            InetAddress addr = InetAddress.getByName(hostname);
            Socket socket = new Socket(addr, port);
        
            // Send header
            String path = "/servlet/SomeServlet";
            BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
            wr.write("POST "+path+" HTTP/1.0\r\n");
            wr.write("Content-Length: "+data.length()+"\r\n");
            wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
            wr.write("\r\n");
        
            // Send data
            wr.write(data);
            wr.flush();
        
            // Get response
            BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                // Process line...
            }
            wr.close();
            rd.close();
        } catch (Exception e) {
        }
      

  4.   

    多谢各位大哥了,还想问一下eureka0891(迷茫中...) ,可以用HttpURLConnection来建立连接吗?