比如我要连接google,端口是80
连接成功后怎么没有返回信息呢!
import java.net.*;
import java.io.*;
public class ConnectTester {
  public static void main(String args[]){
    String host="www.google.com";
    int port=80;    new ConnectTester().connect(host,port);
  }
  public void connect(String host,int port){
    SocketAddress remoteAddr=new InetSocketAddress(host,port);
    Socket socket=null;
    String result="";
    try {
        long begin=System.currentTimeMillis();
        socket = new Socket();
        socket.connect(remoteAddr,5000);  
        long end=System.currentTimeMillis();
        System.out.println("___"+socket);
        BufferedReader read=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String msg=null;
        while((msg=read.readLine())!=null){
         System.out.println("FH:"+msg);   //此处没有输出信息。
         break;
        }
        result=(end-begin)+"ms";  
    }catch (IOException e) {
        result="failure";
    } finally {
        try {
            if(socket!=null)socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    System.out.println(remoteAddr+" : "+result);
  }
}

解决方案 »

  1.   

    答:楼主是用TCP直接连接google的,那么你要在TCP流上向google发送一个HTTP的请求报文,它才会响应啊。
    如:你要向它发送类似这样的信息:
    GET /index.html HTTP/1.1
    Accept:image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/x-shockwave-flash,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/*
    Accept-Language:zh-cn
    Accept-Encoding:gzip,deflate
    User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)
    Host:www.google.com:80
    Connection:close以上仅是一个参考
      

  2.   

    答:楼主是用TCP直接连接google的,那么你要在TCP流上向google发送一个HTTP的请求报文,它才会响应啊。 
    如:你要向它发送类似这样的信息: 支持这个,你需要先请求信息,然后再获取返回的信息。
      

  3.   

    我在IE里面设置代理,在IE中输入网址后,通过我设定的代理来访问网站。
      

  4.   

    你的是客户端的socket服务器端的socket还没有接收你的请求呢
      

  5.   

    现在我将IE里面的代理服务器设置为127.0.0.1 端口是8000;
    我监听这个端口,如果有请求连接时,我解析发出的请求,然后在转发给服务器。服务器接受我发出的请求后,应该返回东西吧?
    下面是我在IE里输入www.baidu.com我加收到的请求
    socket::/127.0.0.1
    GET http://www.baidu.com/ HTTP/1.0
    Accept: application/x-shockwave-flash, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
    Accept-Language: zh-cn
    Cookie: BAIDUID=C54201079ECC4B740F9012A0A4F0588F:FG=1; BDSTAT=964a44ca5bdcf2ea0cf431adcbef76094b36acaf2cdda3cc7cd98d1000e95c68
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
    Host: www.baidu.com
    Proxy-Connection: Keep-Alive我把这些发送出去,应该就能够连上baidu 了吧。
    下面是我写的代码,大侠看下package com.xuantian.socket;import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketTimeoutException;public class JabberServer { public static final int PORT = 8000; /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
    ServerSocket s = new ServerSocket(PORT);
    System.out.println("Started: " + s);
    try {
    Socket socket = s.accept();
    try {
    System.out.println("Connection accepted: " + socket);
    System.out.println("socket::"+socket.getInetAddress());
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    PrintWriter out = new PrintWriter(new BufferedWriter(
    new OutputStreamWriter(socket.getOutputStream())), true);
    String str=null;

    while((str=in.readLine())!=null){
    System.out.println(str);
    out.println(str);
    }

    } finally {
    System.out.println("closing...");
    socket.close();
    }
    } finally {
    s.close();
    }
    }
    }
      

  6.   

    commons-httpclient-3.1.zip
    楼主去找找这个开源的项目,它可以模拟浏览器向服务器发送GET POST之前的请求,
    并得到应答进行处理.