首先,我已经看过几个文档,有关相同的stackoverflow问题..我的项目特定问题 尝试运行命令时:
curl -u username:password https://example.com/xyz/abc
String username="myusername";
String password="mypassword";
String url="https://www.example.com/xyz/abc";
String[] command = {"curl", "-u" ,"Accept:application/json", username, ":" , password , url};
ProcessBuilder process = new ProcessBuilder(command);
Process p;
try
{
p = process.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
System.out.print(result);
}
catch (IOException e)
{ System.out.print("error");
e.printStackTrace();
}
我收到了未经授权的401错误和一堆html标签。 这似乎是一个重复的问题,但我已经尝试了所有的方法。 我知道替代方法是使用http响应方法,但特别是我想使用curl命令。 提前致谢。
答案 0 :(得分:13)
尝试更改此行
String[] command = {"curl", "-u" ,"Accept:application/json", username, ":" , password , url};
到
String[] command = {"curl", "-H", "Accept:application/json", "-u", username+":"+password , url};
答案 1 :(得分:1)
String[] command = {"curl", "-u" , username+ ":" + password , url};