I am sending Https request with username and password through 2 ways but in both the cases I am getting Error 401- Unauthorized error
direct connection through URLConnection
try { URL url = new URL("https://xxx.xxxxx.com/v1/limit/5"); URLConnection urlConnection = url.openConnection(); HttpsURLConnection connection = null; if(urlConnection instanceof HttpsURLConnection) { String authStr = "username"+":"+"password"; byte[] bytesEncoded = Base64.encodeBase64(authStr.getBytes()); String authEncoded = new String(bytesEncoded); connection = (HttpsURLConnection) url.openConnection(); connection.setRequestProperty("Authorization", "Basic "+authEncoded); } else { System.out.println("Please enter an HTTPs URL."); return; } BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String urlString = ""; String current; while((current = in.readLine()) != null) { urlString += current; } System.out.println(urlString); } catch(IOException e) { e.printStackTrace(); }
Connection through Jersey Client but in the same way I am getting 401-unauth error