无需在Java中打开浏览器即可运行url

时间:2018-05-08 06:17:46

标签: java url browser

我正在尝试运行一个URL命令,该命令假设从我的软件调用到手机,它的工作原理就是我想要它没有浏览器或者使用它并在3-5秒后关闭它也很好由我。 所以基本上有String包含:URL +电话号码 现在我尝试了很多选项,如:

    HttpURLConnection httpConn = null;
    URL url = new URL(url_open);
    httpConn = (HttpURLConnection) url.openConnection();

    httpConn.setRequestMethod("GET");
    // Time out after a 5 seconds
    httpConn.setConnectTimeout(5000);
    httpConn.connect();
    httpConn.disconnect();

我试过了:

Process p = Runtime.getRuntime().exec("C:\\Program Files\\Internet 
Explorer\\iexplore.exe" +" " + url_open);
Thread.sleep(5000);
System.out.println("The address" + " " + url_open);
p.destroy();

没有成功。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

        String url = "<Your URL>";      
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        //add request header
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        int responseCode = con.getResponseCode();

        if(responseCode == 200){
              System.out.println("Done!!");     
         }