Hi all , could anyone give me a httpPut request example code?
+1
A:
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();
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);
I'm pretty sure this should work though I haven't tested it :)
BeRecursive
2010-09-06 08:11:38
thx a lot , could you give me a HttpPut class example ^^"?
peterlawn
2010-09-06 08:31:19
I think that's correct, that's how you do a POST, I've never actually tried a PUT before
BeRecursive
2010-09-06 09:04:18
is the setEntity used to pass data? As in out.write(...) in the first example? If yes, how do I pass simple string to it?
Codevalley
2010-10-05 16:54:47