http PATCH 127.0.0.1:8080/db/col/*?filter={'__i
d':{'$oid':'577b07800385b141c4d13ad1'},'submenu.subcode':'B013'} "submenu.$.subname"="test123"
现在要将上面的httpie 语句用httpclient 改写,我写的代码如下:
public static String getContentByPara(String url){
String result= "";
DefaultHttpClient client = new DefaultHttpClient();
HttpPut httppatch = new HttpPut("127.0.0.1:8080/db/col/*");
httppatch.setHeader("Content-Type", "application/hal+json");
JSONObject para = new JSONObject();
para.put("submenu.$.subname", "test123");
try {
List<NameValuePair> temp = new ArrayList<NameValuePair>();
temp.add(new BasicNameValuePair("filter", "{'_id':{'$oid':'577b07800385b141c4d13ad1'},'submenu.subcode':'B013'}"));
String str = EntityUtils.toString(new UrlEncodedFormEntity(temp,"UTF-8"));
httppatch.setEntity(new StringEntity(para.toString(),"UTF-8"));
httppatch.setURI(new URI(httppatch.getURI().toString() + "?"+ str));
HttpResponse httpResponse = client.execute(httppatch);
System.out.println(httpResponse.getStatusLine().toString());
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
// System.out.println(result);
}else{
httppatch.abort();
}
} catch (ClientProtocolException e1){
e1.printStackTrace();
}catch (IOException e2){
e2.printStackTrace();
}catch (URISyntaxException e) {
e.printStackTrace();
}finally {
httppatch.releaseConnection();
}
return null;
}
但是报HTTP/1.1 405 Method Not Allowed