简体   繁体   中英

UnknownHostException in socket

I'm making a program that reads html from a website and pulls some things from the html. When I try to start my socket I get the UnknownHostException error. It happens when I call the InetAdress method as well. I've tried multiple ways of doing this including not using the inetaddress method and it doesn't help anything. Here's what the code looks like

try {
    InetAddress add = InetAddress.getByName(text);
} catch (UnknownHostException e1) { 

}

try {
    Socket socket = new Socket(text,80); 
    PrintWriter out = new PrintWriter(socket.getOutputStream()); 
}

Make sure you're passing just the hostname, and not the complete path to the resource you're trying to fetch. For instance, in your comment you wrote en.wikipedia.org/wiki/The_Elder_Scrolls_IV:_Oblivion ; to determine the host your Socket should connect to, pass just en.wikipedia.org to the InetAddress.getByName() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM