我正在尝试用java编写一个postcurl。 我的 curl 是:
curl -X PUT -u username:password http://localhost:1234/api/2.0/data1/include/value1
我用java写的:
String stringUrl = "http://localhost:1234/api/2.0/data1/include/value1";
URL url = new URL(stringUrl);
URLConnection uc = url.openConnection();
uc.setRequestProperty("X-Requested-With", "Curl");
String userpass = "username" + ":" + "password";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);
InputStreamReader inputStreamReader = new InputStreamReader(uc.getInputStream());
有趣的是,它没有给出任何错误,但什么也没发生,并且 value1 没有添加到输入 1,因此这意味着我写的curl 帖子没有执行任何操作。任何人都可以帮助我将上面的 postcurl 请求转换为 java 代码吗?
最佳答案
为了更好地调用 HTTP 方法,请使用 Apache HttpClient。
这里很好地概述了如何开始使用 get 和 post 方法: http://www.vogella.com/tutorials/ApacheHttpClient/article.html
关于java - 用java写一个postcurl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24657056/