Java www.google.com:HTTP/1.0 302找到了。真的吗?如果我是从主方法还是不从主方法来做这件事,会有区别吗?我目前正在…您正在使用上面的确切代码?@gonzoc0ding我正在main方法中使用上面的代码。我刚刚省略了try/catch块,

Java www.google.com:HTTP/1.0 302找到了。真的吗?如果我是从主方法还是不从主方法来做这件事,会有区别吗?我目前正在…您正在使用上面的确切代码?@gonzoc0ding我正在main方法中使用上面的代码。我刚刚省略了try/catch块,,java,sockets,Java,Sockets,www.google.com:HTTP/1.0 302找到了。真的吗?如果我是从主方法还是不从主方法来做这件事,会有区别吗?我目前正在…您正在使用上面的确切代码?@gonzoc0ding我正在main方法中使用上面的代码。我刚刚省略了try/catch块,让该方法抛出异常。我还添加了行声明。仍然不行-只是在我键入“www.google.com”并按enter键后挂起。我搞不懂。使用上面的代码会导致挂起。我们的环境之间有什么不同?我没有防火墙(我知道),只是在运行OSX…你确定它挂起了吗?如果您添


www.google.com:HTTP/1.0 302找到了。真的吗?如果我是从主方法还是不从主方法来做这件事,会有区别吗?我目前正在…您正在使用上面的确切代码?@gonzoc0ding我正在
main
方法中使用上面的代码。我刚刚省略了
try
/
catch
块,让该方法抛出异常。我还添加了
声明。仍然不行-只是在我键入“www.google.com”并按enter键后挂起。我搞不懂。使用上面的代码会导致挂起。我们的环境之间有什么不同?我没有防火墙(我知道),只是在运行OSX…你确定它挂起了吗?如果您添加任何其他System.out.println(例如,在out.pringln(“GET…)-您看到它打印什么了吗?我一直看到println,直到我尝试读取服务器响应的一行:System.out.println(“来自“+line+”的响应:“+in.readLine());例如,如果我在最后一行之前打印某个内容,我会看到它。此外,如果我删除”+在程序的最后一行的.readLine()中,它显示了“来自www.google.com的响应:”。仍然不行-只是在我键入“www.google.com”并按enter键后挂起。我无法理解。使用上面的确切代码会导致挂起。我们的环境之间有什么不同?我没有防火墙(我知道)我正在运行OSX…你确定它挂起了吗?如果你添加任何其他System.out.println(例如,就在out.pringln(“GET…)-你看到它打印的内容了吗?我一直看到println,直到我尝试读取一行服务器响应:System.out.println(“来自“+line+”:“+in.readLine())的响应)例如,如果我在最后一行之前打印某个内容,我会看到它。此外,如果我从程序的最后一行删除“+in.readLine()”,它会显示“www.google.com的响应:”。就是这样。非常感谢!就是这样。非常感谢!
import java.io.*;
import java.net.*;

public class DNSTest {
    // Constructor
    public DNSTest() { }

    // Builds GET request, opens socket, waits for response, closes
    public static void main(String[] args) throws Exception{
        String line;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //For each hostname
        while ((line = br.readLine()) != null){ 
            //Resolve the hostname to an IP address
            InetAddress ip = InetAddress.getByName(line);

            //Open socket on ip address
            Socket socket = new Socket(ip, 80);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            //Send request
            out.println("GET /index.html HTTP/1.0\n");

            //Read one line of input
            System.out.println("Response from "+line+": "+in.readLine());

        }
    }   
}
  out.flush();
        Socket socket = new Socket(ip, 80);
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));

        //Send request
        out.println("GET /index.html HTTP/1.0\n");
        out.flush();

        //Read one line of input
        System.out.println("Response from "+line+": "+in.readLine());
 Response from google.com: HTTP/1.0 200 OK
        HttpURLConnection connection = (HttpURLConnection) 
            new URL(url).openConnection();
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.setDoOutput(false);
        connection.setUseCaches(false);
        connection.setRequestProperty("Accept", "text/html");
        final InputStream is = connection.getInputStream();
        // construct BufferredStreamReader from the is
// send request
out.print("GET /index.html HTTP/1.0\r\n\r\n");
out.flush();
// read one line of input
System.out.println("Response from " + line + ": " + in.readLine());