public static String Geturl(String sUrl) {
try
{
String sStr = "";
if (sUrl.indexOf("://")< 0) sUrl = "http://" + sUrl;
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
//设置代理
//httpClient.getHostConfiguration().setProxy(hostName,port);
//创建GET方法的实例
GetMethod getMethod = new GetMethod(sUrl);
//使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
getMethod.getParams().setContentCharset("gb2312"); // getMethod.addRequestHeader("Content-Type", "text/html; charset=gb2312"); try {
//执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
//读取内容
//byte[] responseBody = getMethod.getResponseBody();
//sStr = new String(getMethod.getResponseBody());
StringBuffer resultBuffer = new StringBuffer();
BufferedReader in = new BufferedReader(
new InputStreamReader(getMethod.getResponseBodyAsStream(),getMethod.getResponseCharSet()));
String inputLine = null;
while((inputLine = in.readLine()) != null)
{
resultBuffer.append(inputLine);
resultBuffer.append("\n");
}
in.close();
sStr = resultBuffer.toString();
//iso-8859-1 is the default reading encode
//sStr = ConverterStringCode(resultBuffer.toString(),
// getMethod.getResponseCharSet(),encode);
} catch (HttpException e) {
//发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address(发生致命的异常,可能是协议不对或者返回的内容有问题)!");
e.printStackTrace();
} catch (IOException e) {
//发生网络异常
e.printStackTrace();
} finally {
//释放连接
getMethod.releaseConnection();
} return sStr;
}
catch(Exception e)
{
System.out.println("取网页内容出错,错误信息:" +e.toString());
return "";
}
}自己看看吧
try
{
String sStr = "";
if (sUrl.indexOf("://")< 0) sUrl = "http://" + sUrl;
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
//设置代理
//httpClient.getHostConfiguration().setProxy(hostName,port);
//创建GET方法的实例
GetMethod getMethod = new GetMethod(sUrl);
//使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
getMethod.getParams().setContentCharset("gb2312"); // getMethod.addRequestHeader("Content-Type", "text/html; charset=gb2312"); try {
//执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
//读取内容
//byte[] responseBody = getMethod.getResponseBody();
//sStr = new String(getMethod.getResponseBody());
StringBuffer resultBuffer = new StringBuffer();
BufferedReader in = new BufferedReader(
new InputStreamReader(getMethod.getResponseBodyAsStream(),getMethod.getResponseCharSet()));
String inputLine = null;
while((inputLine = in.readLine()) != null)
{
resultBuffer.append(inputLine);
resultBuffer.append("\n");
}
in.close();
sStr = resultBuffer.toString();
//iso-8859-1 is the default reading encode
//sStr = ConverterStringCode(resultBuffer.toString(),
// getMethod.getResponseCharSet(),encode);
} catch (HttpException e) {
//发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address(发生致命的异常,可能是协议不对或者返回的内容有问题)!");
e.printStackTrace();
} catch (IOException e) {
//发生网络异常
e.printStackTrace();
} finally {
//释放连接
getMethod.releaseConnection();
} return sStr;
}
catch(Exception e)
{
System.out.println("取网页内容出错,错误信息:" +e.toString());
return "";
}
}自己看看吧
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货