MediaWikiサーバー(具体的にはwikia.com)で記事を読み書きするためのコードをいくつか書きました。編集トークンを取得して記事を読むことができますが、問題ありません。しかし、記事を書き込もうとすると、次のエラーが発生します。
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at org.apache.http.impl.io.AbstractSessionOutputBuffer.flushBuffer(AbstractSessionOutputBuffer.java:106)
at org.apache.http.impl.io.AbstractSessionOutputBuffer.writeLine(AbstractSessionOutputBuffer.java:198)
at org.apache.http.impl.io.HttpRequestWriter.writeHeadLine(HttpRequestWriter.java:61)
at org.apache.http.impl.io.AbstractMessageWriter.write(AbstractMessageWriter.java:93)
at org.apache.http.impl.AbstractHttpClientConnection.sendRequestHeader(AbstractHttpClientConnection.java:240)
at org.apache.http.impl.conn.DefaultClientConnection.sendRequestHeader(DefaultClientConnection.java:252)
at org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestHeader(AbstractClientConnAdapter.java:227)
at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:213)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:483)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
at com.walkertribe.niftybot.wiki.MediaWiki.loadXML(MediaWiki.java:350)
... 3 more
以下は、記事を書き込もうとするコードの簡略版です。サーバーに対してリクエストを行うためにApacheHttpClientを使用しています。PageクラスはPOJOであり、特に、記事のタイトルと最終改訂時刻を提供します。URIBuilderクラスは、URLを作成するための便利なクラスです。
public void savePage(Page page, String content, String summary) {
if (_token == null) {
throw new IllegalStateException("No edit token received");
}
String md5;
try {
md5 = Util.md5(content);
} catch (UnsupportedEncodingException | NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
}
URIBuilder b = _config.buildURIBuilder()
.addQueryParam("title", page.getTitle())
.addQueryParam("text", content)
.addQueryParam("summary", summary)
.addQueryParam("bot", null)
.addQueryParam("basetimestamp", page.getLastRevision())
.addQueryParam("watchlist", "nochange")
.addQueryParam("md5", md5)
.addQueryParam("token", _token)
.addQueryParam("starttimestamp", _timestamp);
URI uri = b.toURI();
Document doc = loadXML(uri, HttpMethod.POST);
/* do stuff with the returned document */
}
private Document loadXML(URI uri, HttpMethod method) throws WikiException {
HttpRequestBase requestBase = method.getMethod(uri);
HttpResponse response;
try {
response = client.execute(requestBase);
} catch (ClientProtocolException ex | IOException ex) {
throw new WikiException(ex);
}
HttpEntity entity = response.getEntity();
if (entity == null) {
throw new WikiException("No content found: " + uri);
}
try (InputStream content = entity.getContent()) {
// do something with the content
} catch (IllegalStateException | IOException ex) {
throw new WikiException(ex);
}
return doc;
}
private enum HttpMethod {
GET {
@Override
public HttpRequestBase getMethod(URI uri) {
return new HttpGet(uri);
}
},
POST {
@Override
public HttpRequestBase getMethod(URI uri) {
return new HttpPost(uri);
}
};
abstract HttpRequestBase getMethod(URI uri);
}