Android RESTFUL请求:
删除 DEL:
//创建一个http客户端
HttpClientclient=newDefaultHttpClient();
//创建一个DELETE请求
HttpDeletehttpDelete=newHttpDelete("http://www.store.com/product/1234");
//向服务器发送DELETE请求并获取服务器返回的结果,可能是删除成功,或者失败等信息
HttpResponseresponse=client.execute(httpDelete);
修改 PUT:
//创建一个http客户端
HttpClientclient=newDefaultHttpClient();
//创建一个PUT请求
HttpPuthttpPut=newHttpPut("http://www.store.com/product/1234");
//组装数据放到HttpEntity中发送到服务器
final ListdataList=newArrayList();
dataList.add(new BasicNameValuePair("price", "11.99"));
HttpEntityentity=newUrlEncodedFormEntity(dataList, "UTF-8");
httpPut.setEntity(entity);
//向服务器发送PUT请求并获取服务器返回的结果,可能是修改成功,或者失败等信息
HttpResponseresponse=client.execute(httpPut);
获取数据GET:
//创建一个http客户端
HttpClientclient=newDefaultHttpClient();
//创建一个GET请求
HttpGethttpGet=newHttpGet("http://www.store.com/products");
//向服务器发送请求并获取服务器返回的结果
HttpResponseresponse=client.execute(httpGet);
//返回的结果可能放到InputStream,http Header中等。
InputStreaminputStream=response.getEntity().getContent();
Header[]headers=response.getAllHeaders();
新增数据 POST:
//创建一个http客户端
HttpClientclient=newDefaultHttpClient();
//创建一个POST请求
HttpPosthttpPost=newHttpPost("http://www.store.com/product");
//组装数据放到HttpEntity中发送到服务器
final ListdataList=newArrayList();
dataList.add(new BasicNameValuePair("productName", "cat"));
dataList.add(new BasicNameValuePair("price", "14.87"));
HttpEntityentity=newUrlEncodedFormEntity(dataList, "UTF-8");
httpPost.setEntity(entity);
//向服务器发送POST请求并获取服务器返回的结果,可能是增加成功返回商品ID,或者失败等信息
HttpResponseresponse=client.execute(httpPost);
IOS RESTFUL:
SVHTTPRequest 获取数据:
[[SVHTTPClient sharedClient] setBasePath:@"http://api.twitter.com/1/"]; [[SVHTTPClient sharedClient] GET:@"users/profile_image" parameters:[NSDictionary dictionaryWithObjectsAndKeys: @"samvermette", @"screen_name", @"original", @"size", nil] completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) { imageCell.image = [[NSImage alloc] initWithData:response]; }];