Reputation: 47
I have a page that is pre-installed on a device and I'm trying to set up a java app that will log-in to use some of the device functionality. The page uses Basic Authentication:
And I tried to log-in via this code:
private HttpURLConnection conn;
String username = "admin";
String password = "admin";
String joinedCredentials = username + ":" + password;
String base64EncodedCredentials = Base64.getEncoder().encodeToString(joinedCredentials.getBytes());
String authorizationHeaderValue = "Basic " + base64EncodedCredentials;
//main Class:
String url = "http://10.10.100.200/";
http.sendPost(url, http.authorizationHeaderValue);
//sendPost(url, postParams) class:
URL obj = new URL(null, url, new sun.net.www.protocol.https.Handler());
//Allow connection without a certificate
TrustManagerManipulator.allowAllSSL();
conn = (HttpURLConnection) obj.openConnection();
conn.setRequestProperty("Authorization", postParams);
//Act like a browser
conn.setUseCaches(false);
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
if (this.cookies != null) {
for (String cookie : this.cookies) {
conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
}
}
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Referer", "http://10.10.100.200/cgi-bin/php/sms-settings.php");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + postParams);
System.out.println("Response Code : " + responseCode);
BufferedReader in =
new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
But I'm getting this Error:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 401 for URL: http://10.10.100.200/
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at java.base/sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1982)
at java.base/sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1977)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:554)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1976)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1544)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1528)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
at sample.EstablishConnectionClass.sendPost(EstablishConnectionClass.java:84)
at sample.EstablishConnectionClass.main(EstablishConnectionClass.java:38)
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://10.10.100.200/ at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1932) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1528) at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:527) at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:308) at sample.EstablishConnectionClass.sendPost(EstablishConnectionClass.java:78) ... 1 more
The error lines refer to:
http.sendPost(url, http.authorizationHeaderValue);
//and
BufferedReader in =
new BufferedReader(new InputStreamReader(conn.getInputStream()));
//and
int responseCode = conn.getResponseCode();
Upvotes: 2
Views: 1853
Reputation: 31
Change this line:
conn.setRequestProperty("Authorization", postParams);
to this:
conn.setRequestProperty("Authorization", authorizationHeaderValue);
Upvotes: 0