目前我使用HttpClient从android连接到.net WEB API,我已经能够进行GET和POST来读/写数据。但是,我想进行更新和删除。
我尝试使用POST执行此操作,但只需创建更多记录。这是我的POST代码,如何将其更改为PUT或DELETE?
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mywebsite.net/api/employees/6");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("firstName", "UpdatedHello"));
nameValuePairs.add(new BasicNameValuePair("lastName", "World"));
nameValuePairs.add(new BasicNameValuePair("employee_name", "UpdatedHello World"));
nameValuePairs.add(new BasicNameValuePair("password", "xxx"));
nameValuePairs.add(new BasicNameValuePair("isActive", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
答案 0 :(得分:1)
是啊! httpClient http://hc.apache.org/httpclient-3.x/methods.html
的文档答案 1 :(得分:0)
您有PutMethod
和DeleteMethod
API用于执行PUT和DELETE Http请求。示例用法如下所述
PUT请求 - put方法非常简单,它需要放置一个URL,并要求将请求方法的主体设置为要上载的数据。可以使用输入流或字符串设置正文。此方法通常在公共服务器上禁用,因为通常不允许客户端将新文件放在服务器上或替换现有文件。
PutMethod put = new PutMethod("http://jakarta.apache.org");
put.setRequestBody(new FileInputStream("UploadMe.gif"));
// execute the method and handle any error responses.
...
// Handle the response. Note that a successful response may not be
// 200, but may also be 201 Created, 204 No Content or any of the other
// 2xx range responses.
删除请求 - 删除方法用于提供URL以删除资源并从服务器读取响应。此方法通常也在公共服务器上禁用,因为它通常是不可取的允许客户端删除服务器上的文件。
DeleteMethod delete = new DeleteMethod("http://jakarata.apache.org");
// execute the method and handle any error responses.
...
// Ensure that if there is a response body it is read, then release the
// connection.
...
delete.releaseConnection();