这似乎是一个奇怪的问题,但我试图在几秒钟的间隔内发送尽可能多的HTTP POST请求.
我想知道是否有人有任何建议,因为我目前只实现了大约6-9个请求,这似乎相当低.我的代码如下 - 我使用的是Apache Commons HTTP库:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
long start = System.currentTimeMillis();
long end = start + 4000;
int count = 0;
while (System.currentTimeMillis() < end)
{
count++;
httpClient.executeMethod(method);
}
System.out.println((double)count/4 + " reqs / sec");
Run Code Online (Sandbox Code Playgroud)
post方法事先创建一次:
String body= getBodyString();
PostMethod method = new PostMethod(Url);
method.setRequestEntity( new StringRequestEntity(body));
method.setRequestHeader(...etc)
Run Code Online (Sandbox Code Playgroud)
我猜是有某种固有的顺序行为,httpclient正在等待响应?在我的情况下,我不关心响应,所以可能有一种方法来提高通话率.也许我可以准备下一个请求,同时还有一个请求等等.还有一个更高效,更快速的库吗?
我是这类代码的新手,所以如果这个问题没有多大意义,我会道歉.