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

问题描述 投票:0回答:1

我正在尝试运行一个URL命令,假设从我的软件调用到手机,它的工作原理就是我想要它而不需要浏览器或者使用它并在3-5秒之后将其关闭它对我来说也没关系。所以基本上有字符串包含: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();

没有成功。

java url browser
1个回答
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!!");     
         }
© www.soinside.com 2019 - 2024. All rights reserved.