1

我正在使用 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。

例如,在放心

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);
}
4

0 回答 0