在发送HTTP请求时遇到多线程 感觉他们还在等待彼此的完成。
因为查询的速度没有留下深刻印象(与C#\ Perl相比)
第一次遇到类似于C#的问题,结果证明问题是通过删除连接限制来解决的
ServicePointManager.DefaultConnectionLimit = 100;
有人能说出我的例子中有什么问题吗? java中是否有这样的限制?
public static String requester(String url, String param, int head, String cook, int ajax) {
HttpClient client = getHttpsClient(new DefaultHttpClient());
HttpPost httppost = new HttpPost(url);
String ans = new String();
if(ajax == 1) {
httppost.setHeader("Content-Type", "application/json;charset=utf-8");
httppost.setHeader("Accept", "application/json");
}
httppost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36");
if(cook != "") {
httppost.setHeader("Cookie", cook);
}
try {
StringEntity se = new StringEntity(param);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = client.execute(httppost);
if(head == 1) {
ans = TextUtils.join("\r\n", response.getAllHeaders());
}
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
ans += builder.toString();
} catch (ClientProtocolException e) {
Log.d("ClientProtocolException","Some Wrong 1");
} catch (IOException e) {
Log.d("ClientProtocolException", e.getLocalizedMessage());
}
return ans;
}
...
public void brute(View v)
{
flag = 0;
for(int i = 0; i < 100; i++)
{
new TheTask().execute();
}
}
class TheTask extends AsyncTask<Void,Void,Void>
{
@Override
protected Void doInBackground(Void... params) {
while(true)
{
..........
public static HttpClient getHttpsClient(HttpClient client) {
try{
X509TrustManager x509TrustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
HttpParams params = new BasicHttpParams();
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(100));
ConnManagerParams.setMaxTotalConnections(params, 10000);
HttpConnectionParams.setSocketBufferSize(params,8192);
HttpConnectionParams.setTcpNoDelay(params, true);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(params, registry);
clientConnectionManager.setMaxTotal (1000);
clientConnectionManager.setDefaultMaxPerRoute (100);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{x509TrustManager}, null);
SSLSocketFactory sslSocketFactory = new ExSSLSocketFactory(sslContext);
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
//ClientConnectionManager clientConnectionManager = client.getConnectionManager();
SchemeRegistry schemeRegistry = clientConnectionManager.getSchemeRegistry();
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
//return new DefaultHttpClient(clientConnectionManager, client.getParams());
return new DefaultHttpClient(clientConnectionManager, params);
} catch (Exception ex) {
return null;
}
}
答案 0 :(得分:0)
请参阅:请参阅http://hc.apache.org/httpclient-3.x/performance.html和http://hc.apache.org/httpclient-3.x/threading.html。
我无法从您的帖子中说出来,但您似乎没有重新使用HttpClient,但您绝对不会给它一个线程安全的连接管理器。根据您的应用,您可能还需要其他性能考虑因素(但在上面的链接中有详细说明)。
此外,根据我个人的轶事证据,HttpClient 4.x通常比HttpClient 3.x更快(再次,你不清楚你正在使用它)。
答案 1 :(得分:0)
<强>解决强>
ConnPerRoute perRoute = new ConnPerRouteBean(100);
ConnManagerParams.setMaxConnectionsPerRoute(params, perRoute);
ConnManagerParams.setMaxTotalConnections(params, 100);
ConnManagerParams.setTimeout(params, 15000);