首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

HttpClient 用来HTTP GET 请求

2012-11-07 
HttpClient 用于HTTP GET 请求HttpClient的使用模式:1. 创建一个HttpClent2.实例化新的HTTP方法,比如PostM

HttpClient 用于HTTP GET 请求

HttpClient的使用模式:

1. 创建一个HttpClent

2.实例化新的HTTP方法,比如PostMethod 或 GetMethod

3.设置HTTP参数名称/值

4.使用HttpClent执行HTTP调用

5.处理Http响应

?

如下代码使用HttpClent获取HttpGet请求:

public class TestHttpGet {public String executeGet(String url) throws Exception {BufferedReader in = null;String content = null;try {// 定义HttpClientHttpClient client = new DefaultHttpClient();// 实例化HTTP方法HttpGet request = new HttpGet();request.setURI(new URI(url));HttpResponse response = client.execute(request);in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));StringBuffer sb = new StringBuffer("");String line = "";String NL = System.getProperty("line.separator");while ((line = in.readLine()) != null) {sb.append(line + NL);}in.close();content = sb.toString();} finally {if (in != null) {try {in.close();// 最后要关闭BufferedReader} catch (Exception e) {e.printStackTrace();}}return content;}}}
? 1 楼 kaki 2012-01-08   有没有方法伪造IP,不然很难突破限制!

热点排行