我正在尝试从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
消息。
显然我对蒸汽以及我发送它的方式处理不当。
答案 0 :(得分:1)
我非常推荐建立在apache http api上的http-request。
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
}
注意:我建议您查看connectTimeout
,socketTimeOut
和connectionRequestTimeout
方法的javadoc。