写入MediaWiki:连接由同行重置

时间:2012-10-03 17:02:22

标签: java http post mediawiki apache-httpclient-4.x

我已经编写了一些代码来在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); 
}

1 个答案:

答案 0 :(得分:0)

想出来。提交的文本很大时,您必须使用MultipartEntity进行后期操作。给定NameValuePair名为params的参数列表以及要发布到的URI:

HttpPost post = new HttpPost(uri);
MultipartEntity entity = new MultipartEntity();

for (NameValuePair param : params) {
    entity.addPart(param.getName(), new StringBody(param.getValue()));
}

post.setEntity(entity);

try {
    HttpResponse response = client.execute(post);
    // do something with the response
} catch (ClientProtocolException ex | IOException ex) {
    // deal with exception
}