簡體   English   中英

寫入MediaWiki:對等重置連接

[英]Writing to MediaWiki: Connection reset by peer

我已經編寫了一些代碼來在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); 
}

弄清楚了。 如果提交的文本較大,則必須對后期操作使用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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM