Problem for Posting an Article to MediaWiki Server

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();
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top