当前位置:首页 > 开发 > 编程语言 > Http > 正文

HTTPClient 简单使用

发表于: 2014-08-15   作者:bo_hai   来源:转载   浏览次数:
摘要: HttpClient 的用模拟http请求的工具,一般用在测试Http的请求,下面是一个简单的例子: public void testHttpClient() throws Exception { String localUrl = "http://127.0.0.1/XXX.html"; //创建HttpClientBuilder HttpClien

HttpClient 的用模拟http请求的工具,一般用在测试Http的请求,下面是一个简单的例子:

	public void testHttpClient() throws Exception {
		String localUrl = "http://127.0.0.1/XXX.html";
		//创建HttpClientBuilder  
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();  
		//HttpClient  
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		HttpPost httpPost = new HttpPost(localUrl);  
		List<NameValuePair> vals = new ArrayList<NameValuePair>();
		// 添加参数
		vals.add(new BasicNameValuePair("Name","bobobo"));
		httpPost.setEntity(new UrlEncodedFormEntity(vals,HTTP.UTF_8));   
		HttpResponse response = closeableHttpClient.execute(httpPost);
		if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
			HttpEntity entity = response.getEntity();  
			System.out.println("status:" + response.getStatusLine());  
			System.out.println("contentEncoding:" + entity.getContentEncoding());  
			System.out.println("response content:" + EntityUtils.toString(entity));
		}
	}

 补充一个更详细实例:

    protected String httpPostWithJSON(String url, String json) {  
        String  body = StringUtils.EMPTY;
        
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();    
        int timeout = 15 * 1000;
        RequestConfig.Builder requestBuilder = RequestConfig.custom()
                .setConnectTimeout(timeout)
                .setConnectionRequestTimeout(timeout)
                .setSocketTimeout(timeout);
        httpClientBuilder.setDefaultRequestConfig(requestBuilder.build());
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();  
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
        try {
            StringEntity se = new StringEntity(json,Consts.UTF_8);
            httpPost.setEntity(se); 
            
            HttpResponse response = closeableHttpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();  
            
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
                body = EntityUtils.toString(entity,Consts.UTF_8);  
            }
        } catch (UnsupportedEncodingException e) {
            logger.error("httpPostWithJSON UnsupportedEncodingException error : ", e);
        } catch (ClientProtocolException e) {
            logger.error("httpPostWithJSON ClientProtocolException error : ", e);
        } catch (IOException e) {
            logger.error("httpPostWithJSON IOException error : ", e);
        } 
        return body;
    }

 

HTTPClient 简单使用

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
1.在eclipse中使用httpclient连接www.163.com网站,如果返回结果OK则判断loadrunner事务成功否则事
使用 HttpClient 和 HtmlParser 实现简易爬虫(一) (2010-04-20 15:30:25) 转载▼ 标签: it 分类:
对于System.Net.Http的学习(二)——使用 HttpClient 进行连接 对于System.Net.Http的学习(一)—
使用 HttpClient 和 HtmlParser 实现简易爬虫(一) (2010-04-20 15:30:25) 转载▼ 标签: it 分类:
HttpClient三种不同的认证方案: Basic, Digest and NTLM. 这些方案可用于服务器或代理对客户端的认
HttpClient三种不同的认证方案: Basic, Digest and NTLM. 这些方案可用于服务器或代理对客户端的认
HttpClient三种不同的认证方案: Basic, Digest and NTLM. 这些方案可用于服务器或代理对客户端的认
HttpClient三种不同的认证方案: Basic, Digest and NTLM. 这些方案可用于服务器或代理对客户端的认
下面的部分内容来源于《Unlocking Android》一书: 要了解HTTPClient,我们先看一下如何使用核心类
HttpClient使用步骤 1、生成代表客户端的HttpClient对象 2、生成代表请求的对象 3、发送请求,获得
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号