我已经编写了一些代码来在 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
下面是尝试编写文章的代码的简化版本。我正在使用Apache HttpClient向服务器发出请求。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);
}