URL无法在java代码中访问

Rac*_*hel 0 java url http

当在java代码中运行时,可从浏览器访问的URL提供404响应代码.

问题是什么???..任何人都可以解释一下这个

public String login(String url) {
        URL targetURL;
        long start = 0;
        long end = 0;
        float difference = 0;
        HttpURLConnection httpURLConnection;
        StringBuffer strbufstatus = new StringBuffer();
        try {
            //Connecting to the url
            targetURL = new URL(url);

            start = System.currentTimeMillis();

            httpURLConnection = (HttpURLConnection) targetURL.openConnection();

            httpURLConnection.setUseCaches(false);

            httpURLConnection.setAllowUserInteraction(false);

            httpURLConnection.setDoInput(true);

            httpURLConnection.setRequestMethod("GET");

            httpURLConnection.connect();

            //Getting the respond Code
            int responseCode = httpURLConnection.getResponseCode();

            strbufstatus.append("Response Code===> " + responseCode + "<br>");

            if(responseCode==200){


            // System.out.println("respondcode===> " + responseCode);

            end = System.currentTimeMillis();

            //Calculating the response time

            difference = (end - start);

            difference = difference / 1000;

            // System.out.println("Response Time===> " + difference);

            strbufstatus.append("Rsponse time===> " + difference + "<br>");
            }
            } catch (IOException ex) {
            if (ex.toString().contains("java.net.UnknownHostException:")) {

                strbufstatus.append(" - UnknownHostException has occured during Httpconnection\n");
            } else if (ex.toString().contains("java.net.MalformedURLException: unknown protocol:")) {
                strbufstatus.append(" - Unknown Protocol\n");
            } else if (ex.toString().contains("java.net.ConnectException: Connection timed out: connect")) {
                strbufstatus.append("Connection TimedOut\n");
            } else {
                strbufstatus.append("IOException has occured during Httpconnection \n");
            }
            ex.printStackTrace();
        }
        System.out.println("Status" +strbufstatus);
        return strbufstatus.toString();


    }
Run Code Online (Sandbox Code Playgroud)

Jig*_*shi 5

以下可能是问题:

  • 错误代码(如果您对此案例有疑问,请打开代码)
  • 你是代理的后面,浏览器已配置它,但你的程序没有
  • 如果您尝试访问安全URL HTTPS,则使用证书配置

  • +1 - 根据我的经验,代理问题是最有可能的问题. (2认同)