Connecting to remote URL which requires authentication using Java

前端 未结 12 2091
庸人自扰
庸人自扰 2020-11-22 12:59

How do I connect to a remote URL in Java which requires authentication. I\'m trying to find a way to modify the following code to be able to programatically provide a userna

相关标签:
12条回答
  • 2020-11-22 13:37

    Use this code for basic authentication.

    URL url = new URL(path);
    String userPass = "username:password";
    String basicAuth = "Basic " + Base64.encodeToString(userPass.getBytes(), Base64.DEFAULT);//or
    //String basicAuth = "Basic " + new String(Base64.encode(userPass.getBytes(), Base64.No_WRAP));
    HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
    urlConnection.setRequestProperty("Authorization", basicAuth);
    urlConnection.connect();

    0 讨论(0)
  • 2020-11-22 13:39

    Be really careful with the "Base64().encode()"approach, my team and I got 400 Apache bad request issues because it adds a \r\n at the end of the string generated.

    We found it sniffing packets thanks to Wireshark.

    Here is our solution :

    import org.apache.commons.codec.binary.Base64;
    
    HttpGet getRequest = new HttpGet(endpoint);
    getRequest.addHeader("Authorization", "Basic " + getBasicAuthenticationEncoding());
    
    private String getBasicAuthenticationEncoding() {
    
            String userPassword = username + ":" + password;
            return new String(Base64.encodeBase64(userPassword.getBytes()));
        }
    

    Hope it helps!

    0 讨论(0)
  • 2020-11-22 13:41

    i did that this way you need to do this just copy paste it be happy

        HttpURLConnection urlConnection;
        String url;
     //   String data = json;
        String result = null;
        try {
            String username ="danish.hussain@gmail.com";
            String password = "12345678";
    
            String auth =new String(username + ":" + password);
            byte[] data1 = auth.getBytes(UTF_8);
            String base64 = Base64.encodeToString(data1, Base64.NO_WRAP);
            //Connect
            urlConnection = (HttpURLConnection) ((new URL(urlBasePath).openConnection()));
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestProperty("Authorization", "Basic "+base64);
            urlConnection.setRequestProperty("Accept", "application/json");
            urlConnection.setRequestMethod("POST");
            urlConnection.setConnectTimeout(10000);
            urlConnection.connect();
            JSONObject obj = new JSONObject();
    
            obj.put("MobileNumber", "+97333746934");
            obj.put("EmailAddress", "danish.hussain@mee.com");
            obj.put("FirstName", "Danish");
            obj.put("LastName", "Hussain");
            obj.put("Country", "BH");
            obj.put("Language", "EN");
            String data = obj.toString();
            //Write
            OutputStream outputStream = urlConnection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            writer.write(data);
            writer.close();
            outputStream.close();
            int responseCode=urlConnection.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                //Read
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
    
            String line = null;
            StringBuilder sb = new StringBuilder();
    
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
    
            bufferedReader.close();
            result = sb.toString();
    
            }else {
            //    return new String("false : "+responseCode);
            new String("false : "+responseCode);
            }
    
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
    0 讨论(0)
  • 2020-11-22 13:45

    ANDROD IMPLEMENTATION A complete method to request data/string response from web service requesting authorization with username and password

    public static String getData(String uri, String userName, String userPassword) {
            BufferedReader reader = null;
            byte[] loginBytes = (userName + ":" + userPassword).getBytes();
    
            StringBuilder loginBuilder = new StringBuilder()
                    .append("Basic ")
                    .append(Base64.encodeToString(loginBytes, Base64.DEFAULT));
    
            try {
                URL url = new URL(uri);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.addRequestProperty("Authorization", loginBuilder.toString());
    
                StringBuilder sb = new StringBuilder();
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = reader.readLine())!= null){
                    sb.append(line);
                    sb.append("\n");
                }
    
                return  sb.toString();
    
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                if (null != reader){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-22 13:48

    I'd like to provide an answer for the case that you do not have control over the code that opens the connection. Like I did when using the URLClassLoader to load a jar file from a password protected server.

    The Authenticator solution would work but has the drawback that it first tries to reach the server without a password and only after the server asks for a password provides one. That's an unnecessary roundtrip if you already know the server would need a password.

    public class MyStreamHandlerFactory implements URLStreamHandlerFactory {
    
        private final ServerInfo serverInfo;
    
        public MyStreamHandlerFactory(ServerInfo serverInfo) {
            this.serverInfo = serverInfo;
        }
    
        @Override
        public URLStreamHandler createURLStreamHandler(String protocol) {
            switch (protocol) {
                case "my":
                    return new MyStreamHandler(serverInfo);
                default:
                    return null;
            }
        }
    
    }
    
    public class MyStreamHandler extends URLStreamHandler {
    
        private final String encodedCredentials;
    
        public MyStreamHandler(ServerInfo serverInfo) {
            String strCredentials = serverInfo.getUsername() + ":" + serverInfo.getPassword();
            this.encodedCredentials = Base64.getEncoder().encodeToString(strCredentials.getBytes());
        }
    
        @Override
        protected URLConnection openConnection(URL url) throws IOException {
            String authority = url.getAuthority();
            String protocol = "http";
            URL directUrl = new URL(protocol, url.getHost(), url.getPort(), url.getFile());
    
            HttpURLConnection connection = (HttpURLConnection) directUrl.openConnection();
            connection.setRequestProperty("Authorization", "Basic " + encodedCredentials);
    
            return connection;
        }
    
    }
    

    This registers a new protocol my that is replaced by http when credentials are added. So when creating the new URLClassLoader just replace http with my and everything is fine. I know URLClassLoader provides a constructor that takes an URLStreamHandlerFactory but this factory is not used if the URL points to a jar file.

    0 讨论(0)
  • 2020-11-22 13:50

    If you are using the normal login whilst entering the username and password between the protocol and the domain this is simpler. It also works with and without login.

    Sample Url: http://user:pass@domain.com/url

    URL url = new URL("http://user:pass@domain.com/url");
    URLConnection urlConnection = url.openConnection();
    
    if (url.getUserInfo() != null) {
        String basicAuth = "Basic " + new String(new Base64().encode(url.getUserInfo().getBytes()));
        urlConnection.setRequestProperty("Authorization", basicAuth);
    }
    
    InputStream inputStream = urlConnection.getInputStream();
    

    Please note in the comment, from valerybodak, below how it is done in an Android development environment.

    0 讨论(0)
提交回复
热议问题