开发者

Add a proxy for URl

开发者 https://www.devze.com 2023-03-09 21:22 出处:网络
How doadd proxy to access a url in java? For aceessing a url I have written a java program which will tell me wether the URl is accessible or not. But it is not working a giving me response code as 40

How do add proxy to access a url in java? For aceessing a url I have written a java program which will tell me wether the URl is accessible or not. But it is not working a giving me response code as 404 though the Url is accessible from the browser. Please help. This is my code

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 pr开发者_JAVA技巧otocol:")) {
            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();



}


Look for specific java System.setProperty's for controlling proxy usage. Some pertinent key names are 'deployment.proxy.type' as well as others listed here.


Old but still working method is like below:

        System.getProperties().put("http.proxySet", "true");
    System.getProperties().put("http.proxyHost", "proxyHostname");
    System.getProperties().put("http.proxyPort", "proxyPort");
    System.getProperties().put("http.proxyUser", username);
    System.getProperties().put("http.proxyPassword", password);
0

精彩评论

暂无评论...
验证码 换一张
取 消