HttpClient使用GET方式通过代理服务器读取页面的例子
2011-09-15 07:27
866 查看
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* HttpClient使用GET方式通过代理服务器读取页面的例子。
*
* @author JAVA世纪网(java2000.net, laozizhu.com)
*/
public class HttpClientGet {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
// 访问的目标站点,端口和协议
HttpHost targetHost = new HttpHost("www.0538qc.com");
// 代理的设置
HttpHost proxy = new HttpHost("10.60.8.20", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
// 目标地址
HttpGet httpget = new HttpGet("/");
System.out.println("目标: " + targetHost);
System.out.println("请求: " + httpget.getRequestLine());
// 执行
HttpResponse response = httpclient.execute(targetHost, httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
// 显示结果
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
if (entity != null) {
entity.consumeContent();
}
}
}
相关文章推荐
- HttpClient使用GET方式通过代理服务器读取页面的例子
- HttpClient读取页面的使用例子
- HttpClient-----1、使用HTTP的GET方式读取网络数据
- HttpClient读取页面的使用例子
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- 通过httpClient4读取页面内容
- Android 网络请求方式之HttpClient,GET,POST,PUT请求使用方法
- HttpClient 4.1.3 初学篇---使用Get和Post模拟登录简单页面(分别带参数)
- (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android 网络请求方式之HttpClient,GET,POST,PUT请求使用方法
- HttpClient的get和post方式提交数据的使用
- Java 通过HttpClient 带证书方式请求Https协议网站 (Post方式带xml文档,Get以及Delete方式)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android程序:使用Http的Get方式读取网络数据
- Java基础知识强化之网络编程笔记18:Android网络通信之 使用HttpClient的Post / Get 方式读取网络数据(基于HTTP通信技术)
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)