java.net.SocketException: Connection reset问题

抓取网页的时候出现这个问题该怎么解决。

// 将URL中的HTML代码下载至本地硬盘
	public static void downloadPageContent(String urlStr)
			throws FileNotFoundException, UnsupportedEncodingException
	{
		int aStatus = 200;
		HttpClient httpclient = new DefaultHttpClient();		
		HttpGet httpget = new HttpGet(urlStr);
		HttpResponse response = null;
		try
		{
			response = httpclient.execute(httpget);
			
		} catch (ClientProtocolException e)
		{
			System.out.println("出错啦。这里");
			// e.printStackTrace();
		} catch (IOException e)
		{
			System.out.println("222");
			if(e instanceof org.apache.http.conn.HttpHostConnectException)
			{
				System.out.println("拒绝链接,应该换IP尝试代理");
				aStatus = 402;
			}
			if (e instanceof java.net.UnknownHostException)
			{
				System.out.println("找不到主机错误发生,处理异常中");
				aStatus = 402;
			}
			aStatus = 402;
		}
		if (aStatus != 402)
		{
			System.out.println(response.getStatusLine().getStatusCode());
			HttpEntity entity = response.getEntity();
			// 如果目录不存在,则创建文件目录
			File file = new File(DirectoryPath);
			if (!file.exists())
				file.mkdirs();// 创建文件
			FileOutputStream os = new FileOutputStream(DirectoryPath
					+ URLEncoder.encode(urlStr, "utf-8"));
			if (entity != null)
			{
				InputStream instream = null;
				try
				{
					instream = entity.getContent();
				} catch (IllegalStateException e)
				{
					System.out.println("出错啦。!");
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				@SuppressWarnings("unused")
				int l;
				byte[] tmp = new byte[2048];
				try
				{
					while ((l = instream.read(tmp)) != -1)
					{
						os.write(tmp, 0, tmp.length);
					}
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			try
			{
				os.close();
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

public static void main(String[] args) throws Exception
	{
		long startTime = System.currentTimeMillis();
		System.out.println(startTime);
		downloadPageContent(“http://www.pooioo.com”);
		long endTime = System.currentTimeMillis();
		System.out.println(endTime);
		System.out.println(endTime-startTime);
		
	}

链接重置。我该怎么办呢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值