来自应用引擎端点的 HttpRequest。连接重置

HttpRequest from app engine endpoint. Connection reset

我正在尝试从 APP Engine 端点发送 HTTP 请求,从 Postman 上的实验我知道结果相当大,请求通常需要大约一分钟。

这是我的代码:

void testRequest() {
    String test = getConnectionString();
    URL url = new URL(YARDI_URL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setInstanceFollowRedirects(false);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "text/xml");
    connection.setConnectTimeout(1000000);
    OutputStream os = connection.getOutputStream();
    PrintWriter p = new PrintWriter(os);
    p.print(test);
    p.close();

    YardiResponse response = new 
    YardiResponse(connection.getInputStream().toString());

    System.out.println(response.getResponse());
    connection.disconnect();
}

我收到两个错误,

第一个是:java.net.ProtocolException: Cannot write output after reading input.

很长一段时间后,我收到一条 java.net.SocketException: Connection reset 消息。

显然我处理蒸汽和发送蒸汽的方式不当。

我强烈推荐 http-request 基于 apache http api。

private static final HttpRequest<String.class> HTTP_REQUEST = 
      HttpRequestBuilder.createPost(YARDI_URL, String.class)
           .responseDeserializer(ResponseDeserializer.ignorableDeserializer())
           .contentTypeOfBody(ContentType.TEXT_XML)
           .connectTimeout(someIntValue)
           .socketTimeOut(someIntValue)
           .connectionRequestTimeout(someIntValue).
           .build();

void testRequest() {
   ResponseHadler<String> yardiHandler = HTTP_REQUEST.executeWithBody(yourXml);

   int statusCode = yardiHandler.getStatusCode();
   String content = yardiHandler.get(); //returns response body as String in this case
}

注意:我建议查看 connectTimeoutsocketTimeOutconnectionRequestTimeout 方法的 javadoc。