Socket client=new Socket("www.sina.com.cn",80);DataOutputStream outToServer=new DataOutputStream(client.getOutputStream());outToServer.writeBytes("GET /index.html/ HTTP/1.1"+'\n'); //发送请求.

BufferedReader inFromServer=
new BufferedReader
(new InputStreamReader(client.getInputStream()));

while(inFromServer.ready())
{
  System.out.println(inFromServer.readLine());
}
client.close();按照Socket工作原理,sina应该给我回复才对噢,为什么就是没反应呢?

解决方案 »

  1.   

    不是Socket工作原理,而是HTTP协议。
    HTTP协议要求HTTP请求要以"\r\n\r\n"结束。
    并且有些HTTP 服务器还要有更多的请求参数,如HOST等。
    你改成
    "GET /index.html/ HTTP/1.1\r\n\r\n"看看。
      

  2.   

    To taolei(实在无聊):
    HTTP/1.1 400 Bad Request不行哦。
      

  3.   

    对,不是这样简单的发个请求就可以的建议用URLConnection 方便多了
      

  4.   

    你要用Socket发送的消息头中还有一些其它内容,比较麻烦,还是使用URLConnection吧:import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;public class Test {
    private HttpURLConnection http = null;
    private URL url = null; public Test() throws Exception {
    url = new URL("http://www.csdn.net");
    BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) {
    System.out.println(line);
    }
    } public static void main(String args[]) {
    try {
    new Test();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  5.   

    多了一个斜杠,另外许多Server需要定义HOST(主要是目前的Application Server都需要支持多主机(host)系统)GET /index.html HTTP/1.1\r\nHOST: www.sina.com.cn\r\n\r\n你要通过Socket来访问HTTP Server,最好还是学习一下HTTP协议。