Android的HttpPut例如codeAndroid、HttpPut、code

2023-09-05 00:05:37 作者:红了脸

任何人都可以给我一个httpPut请求,例如code?

could anyone give me a httpPut request example code?

推荐答案

假设你想使用一个HttpURLConnection类,执行HTTP PUT您使用以下方法:

Assuming you want to use an HttpURLConnection, to perform an HTTP PUT you use the following:

URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(
    httpCon.getOutputStream());
out.write("Data you want to put");
out.close();

要使用HTTPPut类,那么尝试:

To use the HTTPPut class then try:

URL url = new URL("http://www.example.com/resource");
HttpClient client = new DefaultHttpClient();
HttpPut put= new HttpPut(url);

List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("key1", "value1"));
pairs.add(new BasicNameValuePair("key2", "value2"));
put.setEntity(new UrlEncodedFormEntity(pairs));

HttpResponse response = client.execute(put);

我是pretty的肯定,这应该工作,虽然我没有测试它:)

I'm pretty sure this should work though I haven't tested it :)