求解答网站全部。。。

&&& 我使用httpclient来爬去固定一些网站的数据信息,由于使用了线程池因此在抓取的时候经常碰到有异常!
Exception in thread "pool-226-thread-200" java.lang.IllegalStateException: Invalid use of SingleClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.
at org.apache.http.impl.conn.SingleClientConnManager.getConnection(SingleClientConnManager.java:199)
at org.apache.http.impl.conn.SingleClientConnManager$1.getConnection(SingleClientConnManager.java:173)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:390)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
at com.hc.HttpClientFactory.httpGet(HttpClientFactory.java:127)
at com.hc.Fetcher.fetchType(Fetcher.java:449)
at com.hc.Fetcher.fetchType(Fetcher.java:466)
at com.hc.Fetcher.fetchType(Fetcher.java:466)
at com.hc.Fetcher.httpByCode(Fetcher.java:286)
at com.hc.Fetcher.FetchHtml(Fetcher.java:72)
at com.thread.FetchHtml.fetchStart(FetchHtml.java:64)
at com.thread.FetchHtml.run(FetchHtml.java:209)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
这里有我的线程池的实现:
logger.debug("启动");
// 获得抓取配置数据
List songsList = songsService.getSongs();
List provList = provService.getProvs();
if (0 & songsList.size() && 0 & provList.size()) {
// 构造一个线程池
ThreadPoolExecutor // 构造一个线程池
producerPool = new ThreadPoolExecutor(40, 80, 1, TimeUnit.SECONDS, new ArrayBlockingQueue&Runnable&(songsList.size() * provList.size()),new ThreadPoolExecutor.CallerRunsPolicy());
// 循环开始抓取数据
for (Object objS : songsList) {
// 得到单条彩铃信息
Map sMap = (Map) objS;
for (Object objP : provList) {
// 得到单个省份抓取配置信息
Map pMap = (Map) objP;
if (sMap.get("TELTYPE").equals(pMap.get("TELTYPE"))) {
FetchHtml fhFetchHtml = new FetchHtml(sMap, pMap);
producerPool.execute(fhFetchHtml);
// 停止线程
producerPool.shutdown();
logger.debug("结束");
这里是我httpclient的配置信息:
import java.io.IOE
import java.util.ArrayL
import java.util.L
import javax.net.ssl.SSLHandshakeE
import org.apache.http.HttpE
import org.apache.http.HttpEntityEnclosingR
import org.apache.http.HttpR
import org.apache.http.HttpR
import org.apache.http.HttpS
import org.apache.http.HttpV
import org.apache.http.NameValueP
import org.apache.http.NoHttpResponseE
import org.apache.http.client.ClientProtocolE
import org.apache.http.client.HttpC
import org.apache.http.client.HttpRequestRetryH
import org.apache.http.client.ResponseH
import org.apache.http.client.entity.UrlEncodedFormE
import org.apache.http.client.methods.HttpG
import org.apache.http.client.methods.HttpP
import org.apache.http.client.methods.HttpRequestB
import org.apache.http.impl.client.DefaultHttpC
import org.apache.http.message.BasicH
import org.apache.http.params.CoreConnectionPN
import org.apache.http.params.CoreProtocolPN
import org.apache.http.protocol.ExecutionC
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpC
import org.apache.http.util.EntityU
import com.anal.UtilC
public class HttpClientFactory {
private static final String CHARSET_GBK = "GBK";
异常自动恢复处理
使用HttpRequestRetryHandler接口实现请求的异常恢复
private static HttpRequestRetryHandler requestRetryHandler = new HttpRequestRetryHandler() {
// 自定义的恢复策略
public synchronized boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
// 设置恢复策略,在发生异常时候将自动重试3次
if (executionCount & 3) {
// 超过最大次数则不需要重试
if (exception instanceof NoHttpResponseException) {
// 服务停掉则重新尝试连接
if (exception instanceof SSLHandshakeException) {
// SSL异常不需要重试
HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
boolean idempotent = (request instanceof HttpEntityEnclosingRequest);
if (!idempotent) {
// 请求内容相同则重试
使用ResponseHandler接口处理响应
HttpClient使用ResponseHandler会自动管理连接的释放
解决了对连接的释放管理
private static ResponseHandler&String& responseHandler = new ResponseHandler&String&() {
// 自定义响应处理
public synchronized String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
String charset = EntityUtils.getContentCharSet(entity) == null ? CHARSET_GBK : EntityUtils.getContentCharSet(entity);
return new String(EntityUtils.toByteArray(entity), charset);
* 获取DefaultHttpClient实例
* @param charset
* 参数编码集, 可空
* @return DefaultHttpClient 对象
public static DefaultHttpClient getDefaultHttpClient(final String charset){
DefaultHttpClient httpclient = new DefaultHttpClient();
ArrayList headers = new ArrayList();
headers.add(new BasicHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"));
headers.add(new BasicHeader("Accept-Language", "zh-cn,en-us,zh-tw,en-gb,"));
headers.add(new BasicHeader("Accept-Charset","gbk,gb2312,utf-8,BIG5,ISO-8859-1;"));
headers.add(new BasicHeader("Connection","Close"));
headers.add(new BasicHeader("Cache-Control","no-cache"));
headers.add(new BasicHeader("User-Agent", "Mozilla/4.0 ( MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; CIBA)"));
httpclient.getParams().setParameter("http.default-headers", headers);
//设置http头信息
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
//模拟浏览器,解决一些服务器程序只允许浏览器访问的问题
httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset == null ? HTTP.UTF_8 : charset);
httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,30000);
httpclient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT,60000);
httpclient.setHttpRequestRetryHandler(requestRetryHandler);
* get方式提交抓取网页
* @param url
* @param charset
* @throws IOException
* @throws ClientProtocolException
* @throws IOException
public static String httpGet(HttpClient httpClient, String url,
String charset) throws ClientProtocolException, IOException {
HttpGet httpget = new HttpGet(url);
String content =
// 发送请求,得到响应
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null && HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
charset = EntityUtils.getContentCharSet(entity) == null ?
CHARSET_GBK : EntityUtils.getContentCharSet(entity);
content = UtilComm.getString(entity.getContent(),charset);
abortRequest(httpget);
* post方式提交抓取网页
* @param url
* @param charset
* @throws IOException
* @throws ClientProtocolException
public static String httpPost(HttpClient httpClient, String url,
String charset) throws ClientProtocolException, IOException {
HttpPost httppost = new HttpPost(url);
// 得到提交的POST值
List&NameValuePair& nvpsList = UtilComm.getNameValuePairs(url);
httppost.setEntity(new UrlEncodedFormEntity(nvpsList, charset));
// 得到返回值
String content =
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null && HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
charset = EntityUtils.getContentCharSet(entity) == null ?
CHARSET_GBK : EntityUtils.getContentCharSet(entity);
content = UtilComm.getString(entity.getContent(),charset);
abortRequest(httppost);
* 释放HttpClient连接
* @param hrb
* 请求对象
* @param httpclient
client对象
public static void abortRequest(final HttpRequestBase hrb){
if (hrb != null && hrb.isAborted()) {
hrb.abort();
public static void shutdown(final HttpClient httpclient) {
if (httpclient != null) {
httpclient.getConnectionManager().shutdown();
经常碰到这类异常,帮忙看看,什么原因造成的呢?
采纳的答案
你多线程是用httpclient用错了,
如果你需要多线程使用httpclient的话,请看这个例子
public class ClientMultiThreadedExecution {
public static void main(String[] args) throws Exception {
// Create and initialize HTTP parameters
HttpParams params = new BasicHttpParams();
ConnManagerParams.setMaxTotalConnections(params, 100);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
// Create and initialize scheme registry
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// Create an HttpClient with the ThreadSafeClientConnManager.
// This connection manager must be used if more than one thread will
// be using the HttpClient.
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(cm, params);
// create an array of URIs to perform GETs on
String[] urisToGet = {
"http://hc.apache.org/",
"http://hc.apache.org/httpcomponents-core/",
"http://hc.apache.org/httpcomponents-client/",
"http://svn.apache.org/viewvc/httpcomponents/"
// create a thread for each URI
GetThread[] threads = new GetThread[urisToGet.length];
for (int i = 0; i & threads. i++) {
HttpGet httpget = new HttpGet(urisToGet[i]);
threads[i] = new GetThread(httpClient, httpget, i + 1);
// start the threads
for (int j = 0; j & threads. j++) {
threads[j].start();
// join the threads
for (int j = 0; j & threads. j++) {
threads[j].join();
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
* A thread that performs a GET.
static class GetThread extends Thread {
private final HttpClient httpC
private final HttpC
private final HttpG
public GetThread(HttpClient httpClient, HttpGet httpget, int id) {
this.httpClient = httpC
this.context = new BasicHttpContext();
this.httpget =
* Executes the GetMethod and prints some status information.
public void run() {
System.out.println(id + " - about to get something from " + httpget.getURI());
// execute the method
HttpResponse response = httpClient.execute(httpget, context);
System.out.println(id + " - get executed");
// get the response body as an array of bytes
HttpEntity entity = response.getEntity();
if (entity != null) {
byte[] bytes = EntityUtils.toByteArray(entity);
System.out.println(id + " - " + bytes.length + " bytes read");
} catch (Exception e) {
httpget.abort();
System.out.println(id + " - error: " + e);
已解决问题
未解决问题终于找到关于求妻求子最全的解答了!_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
终于找到关于求妻求子最全的解答了!
上传于||文档简介
&&很​全​面​的​求​妻​求​子​方​法​!
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
下载文档到电脑,查找使用更方便
还剩6页未读,继续阅读
你可能喜欢过点作于,根据求出,再根据即可求出;若以,,为顶点的三角形与以,,为顶点的三角形相似,则必有一个角是直角.分两种情况讨论:当时,求出,再根据在中,得出,从而得到,当时,求出,根据且,得出与不相似.先求出,再分两种情况讨论:当时,作的垂直平分线交于,交于;作的垂直平分线交于,交于,连结,在中求出,,,在中,求出,在中,求出,最后根据代入计算即可.当时,,最后根据即可得出的最小值.
解:过点作于,在中,,,,.存在.若以,,为顶点的三角形与以,,为顶点的三角形相似,则必有一个角是直角.当时,在中,,,,.又由知,在中,,,,,存在与相似,此时.当时,在中,,,,,.则且,此时与不相似.如图,因为外接圆的直径为斜边,则,当时,作的垂直平分线交于,交于;作的垂直平分线交于,交于,连结.则为外接圆的半径.在中,,,,,.在中,.在中,,.当时,也成立,.当时,取得最小值.
此题考查了相似形综合,用到的知识点是相似三角形的性质与判定,二次函数的最值,勾股定理,关键是根据题意画出图形构造相似三角形,注意分类讨论.
4002@@3@@@@相似形综合题@@@@@@266@@Math@@Junior@@$266@@2@@@@图形的相似@@@@@@53@@Math@@Junior@@$53@@1@@@@图形的变化@@@@@@7@@Math@@Junior@@$7@@0@@@@初中数学@@@@@@-1@@Math@@Junior@@
第六大题,第1小题
求解答 学习搜索引擎 | 如图,在直角梯形ABCD中,AB//CD,AD垂直于AB,角B={{60}^{\circ }},AB=10,BC=4,点P沿线段AB从点A向点B运动,设AP=x.(1)求AD的长;(2)点P在运动过程中,是否存在以A,P,D为顶点的三角形与以P,C,B为顶点的三角形相似?若存在,求出x的值;若不存在,请说明理由;(3)设\Delta ADP与\Delta PCB的外接圆的面积分别为{{S}_{1}},{{S}_{2}},若S={{S}_{1}}+{{S}_{2}},求S的最小值.}

我要回帖

更多关于 求解答 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信