我用socket访问https.代码到最后就等在那里要很长时间才读完。我的代码如下:  Writer out=null;
  InputStream ina=null;
  BufferedReader in=null;
  Socket socket=null;
  try {
    SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    String tunnelHost = "www.aa.com.cn";
    int tunnelPort = 8443;
    Socket tunnel = new Socket(tunnelHost, tunnelPort);
    socket  = factory.createSocket(tunnel, tunnelHost,tunnelPort, true);
    out = new OutputStreamWriter(socket.getOutputStream(),"GBK");
    out.write("POSE https://www.aa.com.cn:8443/spservice/spgetringinfo.do?spid=xxx&sppwd=xxxx HTTP/1.1\r\n");
    out.write("Host: " + "61.112.123.23" + ":" + 443 + "\r\n");
    out.write("\r\n");
    out.flush();
    ina = socket.getInputStream();
    in = new BufferedReader(new InputStreamReader(ina, "GBK"));
    String line = null;
    String str = in.toString();
----〉〉〉在这里每次读到返回值最后的时候就特别慢。前面readline()都读得特别快!不知道为什么?
    while ((line = in.readLine()) != null) {
System.out.println(line);
    }
  }catch(Exception e){
    System.out.println("error +"+e);
  }finally {
    if(in!=null)
    in.close();
    if(ina!=null)
       ina.close();
    if(out!=null)
      out.close();
    socket.close();
  }

解决方案 »

  1.   

    HTTP本来就是看起来一行一行的,
    另外回车那里不要写 \r\n, Just \n
    第一,\r非Linux可以理解。第二Windows理解\n,忽略\r是完全没有问题的。
      

  2.   

    line = in.readLine() 只有等到对方关闭了 socket 才会返回 null,否则就一直 block 在那里。当然,如果读到 '\n' 就会先返回一个 String,但你的程序马上就又开始了下一个 readLine(),所以,最后一次等很长时间,实际上是在等对方“超时后关闭 socket”。
      

  3.   

    ilovechao1314(haha)why 443?not 80?443是HTTPS的端口,80是HTTP的端口
      

  4.   

    当你读到最后的时候要关闭资源
    finally {
        if(in!=null)
        in.close();
        if(ina!=null)
           ina.close();
        if(out!=null)
          out.close();
        socket.close();
      }
    所以会慢