I have a command line:
curl -u username:passwd -k "https://myserver/a/b/c"
It works and I will get right information when I call it under Ubuntu or Cygwin.
Now I am willing to accomplish it in Java. So I have Java code like that:
public class Test {
public static String auth = "username:passwd";
public static String url = "https:/myserver/a/b/c";
public static void main(String[] args) {
try {
URL url = new URL(url);
final byte[] authBytes = auth.getBytes(StandardCharsets.UTF_8);
String encoding = java.util.Base64.getEncoder().encodeToString(authBytes);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", encoding);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
But It seems not work and always return HTTP response code 411. Is there something wrong with the java code?
Thanks in advanced.
curldoes? Otherwise you're comparing apples and oranges.