URL请求响应(Socket)

package com.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

public class SocketTest {
public static void main(String[] args) throws IOException {
/*System.out.println("本机信息======");
// 获取本地主机的InetAddress实例
InetAddress address = InetAddress.getLocalHost();
// 获取计算机名
String hostName = address.getHostName();
// 获取IP地址
byte[] ip = address.getAddress();
System.out.println("主机名:" + hostName);
System.out.print("ip地址:" + ip[0]+ip[1]+ip[2]+ip[3]+" ");
System.out.println("其他机器信息======");
InetAddress othersAddress = InetAddress.getByName("www.runoob.com");
byte[] othersHostName = othersAddress.getAddress();
System.out.println(othersHostName[1]);*/
System.out.println("url测试======");
URL url = new URL("http://www.baidu.com");
InputStream is = url.openStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String data = br.readLine();
while(data != null) {
System.out.println(data);
data = br.readLine();
}
br.close();
isr.close();
is.close();
}
}

原文地址:https://www.cnblogs.com/xint/p/9013747.html