package untitled3;import java.util.*;
import java.net.*;
import java.io.*;
import java.io.InputStream;public class Untitled1 {
  public Untitled1() {
  }  public static void main(String[] args) throws Exception,
      IOException {
    Socket socket = new Socket("www.sina.com", 80);
    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.
        getInputStream()));
    StringBuffer retBuff = new StringBuffer();
    String line = reader.readLine();
    while (line != null) {
      retBuff.append(line + "\r\n");
      System.out.println(line);
      line = reader.readLine();    }
    System.out.println(retBuff.toString());
  } 
  public static String toChina(String str) {    if (str == null) {
      return null;
    }
    try {      byte[] bytesStr = str.getBytes("ISO-8859-1");
      return new String(bytesStr, "gb2312");    }
    catch (Exception e) {
      e.printStackTrace();
      return "";
    }  }  public static String chinaTo(String str) {
    if (str == null) {
      return null;
    }
    try {
      byte[] bytesStr = str.getBytes("gb2312");
      return new String(bytesStr, "ISO-8859-1");    }
    catch (Exception err) {
      return str;
    }
  }}

解决方案 »

  1.   

    package untitled3;import java.util.*;
    import java.net.*;
    import java.io.*;
    import java.io.InputStream;public class Untitled1 {
      public Untitled1() {
      }//  public static void main(String[] args) throws Exception,
    //      IOException {
    //    Socket socket = new Socket("www.sina.com", 80);
    //    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.
    //        getInputStream()));
    //    StringBuffer retBuff = new StringBuffer();
    //    String line = reader.readLine();
    //    while (line != null) {
    //      retBuff.append(line + "\r\n");
    //      System.out.println(line);
    //      line = reader.readLine();
    //
    //    }
    //    System.out.println(retBuff.toString());
    //  }  public static String toChina(String str) {    if (str == null) {
          return null;
        }
        try {      byte[] bytesStr = str.getBytes("ISO-8859-1");
          return new String(bytesStr, "gb2312");    }
        catch (Exception e) {
          e.printStackTrace();
          return "";
        }  }  public static String chinaTo(String str) {
        if (str == null) {
          return null;
        }
        try {
          byte[] bytesStr = str.getBytes("gb2312");
          return new String(bytesStr, "ISO-8859-1");    }
        catch (Exception err) {
          return str;
        }
      }  public static void main(String[] args) throws UnknownHostException,
          IOException {
        Socket socket = new Socket("www.sina.com.cn", 80);
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.
            getInputStream()));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        out.println("GET / HTTP/1.1");
        out.println("Accept: */*");
        out.println("Accept-Language: zh-cn");
        out.println("Accept-Encoding: gzip, deflate");
        out.println("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; MyIE2)");
        out.println("Host: www.sina.com.cn");
        out.println("Connection: Keep-Alive");
        out.println("");
        out.flush();
        StringBuffer retBuff = new StringBuffer();
        String line = reader.readLine();
        while (line != null) {
          retBuff.append(line + "\r\n");
          System.out.println(line);
          line = reader.readLine();    }
        System.out.println(toChina(retBuff.toString()));
        socket.close();  }}
      

  2.   

    这样可以但不明白为什么啊!
    我就是不明白为什么开始赋了一个初始值BufferedReader reader = new BufferedReader(new InputStreamReader(socket.
            getInputStream()));而且后面又没有改变这个初始值
    而且
    开始的时候这个reader 初始值是什么都没有
    然后执行用out.println执行了一些指令以后竟然就可以读出来东西
     过程中也没有重新赋值给reader 啊!
    socket.getInputStream()因为发了out.println指令后有了
    数值可是一直也没有改变reader的内容阿程序没有任何地方重新给reader赋值但是reader确得到了发out.println指令后的内容
    很奇怪有人知道是为什么吗?
      

  3.   

    你的reader是绑定到www.sina.com:80端口上的。是跟新浪http服务器进行数据交互。根据http协议,你不向服务器发送数据,服务器当然不会理你,也就读不出任何数据。out.println向服务器发出了指令,服务器做出响应,然后你的reader才能收到数据,这个很正常嘛,有啥不明白的?
      

  4.   

    试试这个方法,,
    我记得一些request后面要跟个\n,还有,Param:Value中间最好也不要有空格 
    public String connect2(String host,String path)throws Exception{
         String port = "80";
         if(host.indexOf(":")!=-1){
             port=host.substring(
              host.indexOf(":")+1
              );
             host = host.substring(0,host.indexOf(":"));
            }
             System.out.println ("host:"+host);
         InetAddress i = InetAddress.getByName(host);
            System.out.println (i.getHostAddress());
            System.out.println ("port:"+port);
           Socket client = new Socket(i,Integer.parseInt(port));
           System.out.println ("Client connected.");
           //DataOutputStream dos = new DataOutputStream();
           OutputStream os = client.getOutputStream();
           PrintWriter pt = new PrintWriter(os,true);
           pt.println("GET /"+path+" HTTP/1.1");
           pt.println("Host:"+host+"\n");
           pt.println("Connection:close\n");
           System.out.println ("Request sended, waiting for response..");
          // BufferedInputStream bis = new BufferedInputStream(client.getInputStream());
           DataInputStream ds = new DataInputStream(client.getInputStream());
           //System.out.println ("Server returned , Receiving data..");
           String line="";
           StringBuffer sb = new StringBuffer();    
           InputStream is = client.getInputStream();
           System.out.println ("Response start...");
           byte[] buf = new byte[512];
           byte[] all = new byte[0]; 
           int read = is.read(buf);
           int k=0;
          // System.out.println ("read:"+read);
           while(read!=-1){
             all = append(buf,all);
          //   System.out.println ("all:"+all.length);
             read = is.read(buf);
           }
           System.out.println ("Response length:"+all.length);
           /*
           while( in.readLine() != null )
           {
               sb.append(in.readLine());
           }             ds.close();
           in.close();
           */
           System.out.println ("Response finished.");   
           //return sb.toString(); 
           return new String(all);
        }
        
        //Append a to b
        private byte[] append(byte[] a,byte[] b){
         int k = b.length+a.length;
         int s = b.length;
         byte[] g = new byte[k];
         System.arraycopy(b,0,g,0,b.length);
         System.arraycopy(a,0,g,s,a.length);
         b = g;
         return g;
        }