Java HttpURLConnection Open getConnection(String urlStr, boolean useProxy)

Here you can find the source of getConnection(String urlStr, boolean useProxy)

Description

get Connection

License

Apache License

Declaration

private static HttpURLConnection getConnection(String urlStr, boolean useProxy) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;

public class Main {
    private static HttpURLConnection getConnection(String urlStr, boolean useProxy) throws IOException {

        Authenticator authenticator = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return (new PasswordAuthentication("fis\\haint51", "qwer4321!".toCharArray()));
            }/*from  ww  w  . j  av a  2 s .  c  om*/
        };
        Authenticator.setDefault(authenticator);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.fis.vn", 8080));

        if (!useProxy) {
            proxy = Proxy.NO_PROXY;
        }

        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);

        return conn;
    }
}

Related

  1. getConnection(String url)
  2. getConnection(URL url)
  3. getConnectionCharset(HttpURLConnection connection)
  4. getConnectionStream(HttpURLConnection con)
  5. openConnection(final String urlString)