Does anyone know how I would go about updating a page using a post request in Java Eclipse with the HttpClient library? Currently this is what I have, but when
I execute it I get a page not found error:
public void update() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://examplepage.xml");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("_action", "<BasicPage><title>New Title</title></BasicPage>"));
nameValuePairs.add(new BasicNameValuePair("_method", "post"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String info = (""+EntityUtils.toString(entity));
System.out.println(info);
System.out.println(response.getEntity().getContent());
System.out.println(response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Your code looks fine. Check that the page URI or alias “examplepage.xml” actually exists or mapped. Check also that it can accept POST requests.