用socket做http请求的收发程序。在服务器程序收到http请求后,每次读socket的输入流读到最后就没反应了,既不抛错,也不继续执行。大家帮我看看是哪里的问题? 困扰我好几天做不下去了。多谢大家。服务器端程序             Socket client = null;// 客户Socket   
                client = serverSocket.accept();// 客户机   
                if (client != null) {   
                    System.out.println("连接到服务器的用户:" + client);   
                    try {   
                        // 第一阶段: 打开输入流   
                        BufferedReader in = new BufferedReader(   
                                new InputStreamReader(client.getInputStream()));   
                        int ch=0;
                        System.out.println("start............................");                        while((ch=in.read())!=-1){
                        
                         System.out.print((char)ch);
                        
                        }                        //程序到这儿就没反应了,这个输出总也输不出来                        
                        System.out.println("end............................");
                

解决方案 »

  1.   

     while((ch=in.read())!=-1)
    是不是另一端一直没写入-1
      

  2.   

    另一端的程序
    /**   
     * 发送HTTP请求   
     *
     * @param urlString   
     * @return 响映对象   
     * @throws IOException   
     */   
    private HttpRespons send(String urlString, String method,Map<String, String> parameters, Map<String, String> propertys)
    throws IOException {
    //定义一个Http连接
    HttpURLConnection urlConnection = null;
    //如果是GET方法且参数不为空,拼参数为param
    if (method.equalsIgnoreCase("GET") && parameters != null) {
    StringBuffer param = new StringBuffer();
    int i = 0;
    for (String key : parameters.keySet()) {
    if (i == 0)
    param.append("?");
    else   
    param.append("&");
    param.append(key).append("=").append(parameters.get(key));
    i++;
    }
    urlString += param;
    }
    URL url = new URL(urlString);
    //打开指定地址连接
    urlConnection = (HttpURLConnection) url.openConnection();
        //设置方法名称
    urlConnection.setRequestMethod(method);

    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.setUseCaches(false);
     
    if (propertys != null)
    for (String key : propertys.keySet()) {
    urlConnection.addRequestProperty(key, propertys.get(key));
    }
     
    if (method.equalsIgnoreCase("POST") && parameters != null) {
    StringBuffer param = new StringBuffer();
    for (String key : parameters.keySet()) {
    param.append("&");
    param.append(key).append("=").append(parameters.get(key));
    }

    urlConnection.getOutputStream().write("".getBytes());

    urlConnection.getOutputStream().write(param.toString().getBytes());
    urlConnection.getOutputStream().flush();
    urlConnection.getOutputStream().close();
    }
     
    return this.makeContent(urlString, urlConnection);
    }
      

  3.   

    用另一端的程序给http://www.126.com发post请求,是能得到返回的
      

  4.   

    用in.readLine()读取也是一样,最后一行直接就读不出来没反应了
      

  5.   

    /**   
     * 发送HTTP请求   
     *
     * @param urlString   
     * @return 响映对象   
     * @throws IOException   
     */   
    private HttpRespons send(String urlString, String method,Map<String, String> parameters, Map<String, String> propertys)
    throws IOException {
    //定义一个Http连接
    HttpURLConnection urlConnection = null;
    //如果是GET方法且参数不为空,拼参数为param
    if (method.equalsIgnoreCase("GET") && parameters != null) {
    StringBuffer param = new StringBuffer();
    int i = 0;
    for (String key : parameters.keySet()) {
    if (i == 0)
    param.append("?");
    else   
    param.append("&");
    param.append(key).append("=").append(parameters.get(key));
    i++;
    }
    urlString += param;
    }
    URL url = new URL(urlString);
    //打开指定地址连接
    urlConnection = (HttpURLConnection) url.openConnection();
      //设置方法名称
    urlConnection.setRequestMethod(method);urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.setUseCaches(false);
     
    if (propertys != null)
    for (String key : propertys.keySet()) {
    urlConnection.addRequestProperty(key, propertys.get(key));
    }
     
    if (method.equalsIgnoreCase("POST") && parameters != null) {
    StringBuffer param = new StringBuffer();
    for (String key : parameters.keySet()) {
    param.append("&");
    param.append(key).append("=").append(parameters.get(key));
    }urlConnection.getOutputStream().write("".getBytes());urlConnection.getOutputStream().write(param.toString().getBytes());urlConnection.getOutputStream().write(-1);//////////////
    urlConnection.getOutputStream().flush();
    urlConnection.getOutputStream().close();
    }
     
    return this.makeContent(urlString, urlConnection);
    }
      

  6.   

    标准Http请求,会在请求头生成一个Content-Length值
    当读完请求头,从请求正文里读取到相应长度的字节,即表示读取完成。
    如果没有Content-Length值,则按照一个平均的超时值,读取超时即认为读取完成。
    而不是傻傻的等着读-1.
    实际上,根本不肯能读到-1.因为-1在java里代表流的结束,对应于Socket等效于Socket断开。如果Socket都断开了,你怎么把返回值发给请求的客户端?难道你另起一个Socket回连回去么?