X
xiejw
I want to post articles to the MediaWiki server via the httpclient of
apache. I used the following code to do that, but failed.
Can anybody help me ? Or did such things successfully?
import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class Post {
public static void main(String[] args) {
HttpClient client = new HttpClient();
PostMethod post = new
postMethod("http://localhost/mediawiki/index.php?title=Haha&action=submit");
post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
post.getParams().setIntParameter(HttpMethodParams.SO_TIMEOUT, 5000);
NameValuePair[] data = {
new NameValuePair("wpTextbox1", "== hi =="),
new NameValuePair("wpSummary",""),
new NameValuePair("wpSection","Save page"),
new NameValuePair("wpEdittime",""),
new NameValuePair("wpSave","Save")
};
post.setRequestBody(data);
try {
int statusCode1 = client.executeMethod(post);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("statusLine>>>" + post.getStatusLine());
post.releaseConnection();
}
}
apache. I used the following code to do that, but failed.
Can anybody help me ? Or did such things successfully?
import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class Post {
public static void main(String[] args) {
HttpClient client = new HttpClient();
PostMethod post = new
postMethod("http://localhost/mediawiki/index.php?title=Haha&action=submit");
post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
post.getParams().setIntParameter(HttpMethodParams.SO_TIMEOUT, 5000);
NameValuePair[] data = {
new NameValuePair("wpTextbox1", "== hi =="),
new NameValuePair("wpSummary",""),
new NameValuePair("wpSection","Save page"),
new NameValuePair("wpEdittime",""),
new NameValuePair("wpSave","Save")
};
post.setRequestBody(data);
try {
int statusCode1 = client.executeMethod(post);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("statusLine>>>" + post.getStatusLine());
post.releaseConnection();
}
}