0

I am trying to connect to a URL and read/download the content from the URL using java code. I am facing the below exception.

Exception in thread "main" java.net.UnknownHostException: myURL

code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class GetURLContent {
    public static void main(String[] args) {

        URL url;

        try {
            // get URL content
            url = new URL("https://myURL");
            URLConnection conn = url.openConnection();
            // open the stream and put it into BufferedReader
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;

            //save to this filename
            String fileName = "C:/data/test.html";
            File file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            //use FileWriter to write file
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            while ((inputLine = br.readLine()) != null) {
                bw.write(inputLine);
            }

            bw.close();
            br.close();
            System.out.println("Done");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Do i need to set anything from my application or in my proxy settings to resolve this error.Any suggestions would be helpful.

user8115347
  • 55
  • 10
  • You must be able to tracert, telnet (or ping, if ICMP is allowed against the target) from the machine you're running your code from, to the destination address. If any of those simple operations don't complete successfully, chances are you're not allowed to hit that endpoint, there's a firewall blocking access or your address is simply wrong. – kolossus Jun 12 '17 at 14:13
  • @kolossus - how to do that? I have given the following command in command prompt: ping myURL and it returnd ping request cannot find host. – user8115347 Jun 12 '17 at 14:58

0 Answers0