HttpClient链接拒绝问题。

先贴上代码

package com.susheng.MoneyMaker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class FetchWebData
{
	private static final String DirectoryPath = com.susheng.MoneyMaker.common.FilePath.DownLoadDataPath;

	// 将URL中的HTML代码下载至本地硬盘
	public static void downloadPageContent(String urlStr) throws Exception
	{
		HttpClient httpclient = new DefaultHttpClient();	
		HttpGet httpget = new HttpGet(urlStr);
		
		
		HttpResponse response = httpclient.execute(httpget);
        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 = entity.getContent();
			@SuppressWarnings("unused")
			int l;
			byte[] tmp = new byte[2048];
			while ((l = instream.read(tmp)) != -1)
			{
				os.write(tmp, 0, tmp.length);
			}
		}
		os.close();
	}

	public String getPageContent(String strUrl)
	{
		try
		{
			// 根据网址strUrl创建URL对象
			URL pageUrl = new URL(strUrl);
			// System.out.println(pageUrl.getFile());
			// 下载网页
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					pageUrl.openStream()));
			String line;
			// 读取网页内容
			StringBuffer pageBuffer = new StringBuffer();
			while ((line = reader.readLine()) != null)
			{
				pageBuffer.append(line);
			}
			return pageBuffer.toString();
		} catch (Exception e)
		{
			System.out.println("自己知道");
		}
		return null;
	}

	public static void main(String[] args) throws Exception
	{
  		downloadPageContent("http://www.yanzhenlou.com");
		
	}
}


在使用HttpClient下载网页内容的时候遇到这个链接拒绝。怎么办。怎么找到异常并且处理呢。求指教,感谢!

在Java中,使用HttpClient库连接SSH协议服务器并不直接支持。HttpClient主要用于HTTP和HTTPS请求,而SSH协议是一种用于安全登录远程服务器的协议,与HTTP无关。 要在Java中使用SSH协议连接服务器,可以使用JSch库。JSch是一个纯Java实现的SSH2协议库,可用于在Java程序中执行SSH操作。 首先,需要在项目中引入JSch的依赖,例如使用Maven的话,在pom.xml文件中添加如下配置: ```xml <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency> ``` 然后,可以使用以下样例代码创建一个SSH连接,执行一些命令: ```java import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public class SSHConnectionExample { public static void main(String[] args) { try { JSch jsch = new JSch(); Session session = jsch.getSession("username", "hostname", 22); session.setPassword("password"); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); // 在SSH连接上执行一些命令 // ... session.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` 以上代码中,"username"是SSH服务器的用户名,"hostname"是SSH服务器的主机名或IP地址,22是SSH服务器默认的端口号,"password"是相应的登录密码。 在session连接成功后,可以通过执行一些命令来与远程服务器进行交互。例如,可以使用session的openChannel方法打开一个Channel并执行命令、读取输出结果、发送指令等。 需要注意的是,连接SSH服务器需要正确的用户名和密码,以及正确配置的SSH服务器。如果有需要,还可以使用密钥文件进行身份验证。 希望以上信息能够帮助到您。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值