2012-10-03 117 views
1

我已经写了一些代码来在MediaWiki服务器(特别是wikia.com)上读写文章。我能够获得编辑标记并阅读文章,没有问题。但是,当我尝试撰写文章时,出现以下错误:写给MediaWiki:连接重置由对方

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); 
} 
+0

任何不使用Java库的理由? https://www.mediawiki.org/wiki/API:Client_code#Java – Nemo

+1

最初我使用的是一个库。不幸的是,当时,我能找到的库不支持我使用的所有MediaWiki功能。 –

回答

0

想通了。当提交的文本很大时,您必须将MultipartEntity用于后期操作。给定名为参数的NameValuePair列表以及发布到的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 
}