我想把Curl写成java:
curl -X PUT -u username:password http://localhost:80/api/client/include/clientID
多数民众赞成我用Google搜索但我的问题是如何将client_id和client的值传递给put,因为它们之间存在/ include。我对如何写卷曲感到有点困惑。任何人都可以帮助我吗?
public String RestPutClient(String url, int newValue, int newValue2) {
// example url : http://localhost:80/api/
DefaultHttpClient httpClient = new DefaultHttpClient();
StringBuilder result = new StringBuilder();
try {
HttpPut putRequest = new HttpPut(url);
putRequest.addHeader("Content-Type", "application/json");
putRequest.addHeader("Accept", "application/json");
JSONObject keyArg = new JSONObject();
keyArg.put("value1", newValue);
keyArg.put("value2", newValue2);
StringEntity input;
try {
input = new StringEntity(keyArg.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return success;
}
putRequest.setEntity(input);
HttpResponse response = httpClient.execute(putRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(response.getEntity().getContent())));
String output;
while ((output = br.readLine()) != null) {
result.append(output);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
答案 0 :(得分:0)
我假设您传递的参数应该代表客户端和clientID。您只需构建从参数传递给HttpPut的URL。
如果这些是你的参数
url = "http://localhost:80/api/";
newValue = "client";
newValue2 = "clientID";
然后您的HttpPut初始化将如下所示
HttpPut putRequest = new HttpPut(url + newValue + "/include/" + newValue2);