我正在使用Apache HTTPClient为RESTful API编写测试,当我执行大量请求(返回超过80,000个字符)时,响应被切断。截止点通常约为85,275。我可以在curl中从命令行运行完全相同的请求并获取整个语句。
我正在运行的系统正在运行Tomcat,服务器日志表明正在返回整个响应,所以在我看来问题就像HTTPClient带回数据一样。
我已使用Rest Assured(使用HTTPClient)和HTTPClient直接完成此操作,以确保问题不在Rest Assured中。我更改了几个参数,包括CoreConnectionPNames参数CONNECTION_TIMEOUT,MAX_LINE_LENGTH,SO_TIMEOUT和SOCKET_BUFFER_SIZE。
例如,在Rest Assured中
given()
.param("areaCode", "515")
.param("quantity", "5000")
.expect()
.statusCode(200)
.body("SearchResults.ResultCount", equalTo("5000"),
"SearchResults.TelephoneNumberList.TelephoneNumber[0]", startsWith("515"),
"SearchResults.TelephoneNumberList.TelephoneNumber[1]", startsWith("515"),
"SearchResults.TelephoneNumberList.TelephoneNumber[2]", startsWith("515"))
.when()
.get("/accounts/1/availableNumbers");
或在HTTPClient中
// I've set up an httpclient with standard stuff to ignore the self-signed
// certs in our development environment
DefaultHttpClient httpclient = getNewHttpClient();
httpclient.getCredentialsProvider().
setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials("xxxx", "xxxx"));
org.apache.http.client.utils.URIBuilder builder =
new org.apache.http.client.utils.URIBuilder();
builder.setScheme("https")
.setHost("api.test.xxxx.com")
.setPath("/accounts/1/availableNumbers")
.setParameter("areaCode", "515")
.setParameter("quantity", "5000");
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
// print out request URI
System.out.println(httpget.getURI());
HttpResponse response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() != 200)
{
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null)
{
System.out.println(output);
}