`
bo_hai
  • 浏览: 554170 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

HTTPClient 简单使用

阅读更多

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简单使用

    基于Java的httpclient简单使用,纯属学习使用,如发生问题,作者不负任何责任。有异议,请联系1218476693@qq.com。

    httpClient组件使用说明

    httpClient组件使用说明,搭建,所需jar包,简单应用,例子。适用于初级学员

    Httpclient使用jar包三合一,基本使用方法

    简单使用方法: public static void main(String[] args) { // String str1 = &quot;http://dev.d-smart.cn/Login&quot;; // http协议路径 String str1 = &quot;&quot;; HttpClient httpClient = new ...

    httpclient4.0 使用帮助、例子

    其中有4.1.1jar包、httpclient说明文档doc格式、简单使用例子post\get方式都有,导入类可用。项目中使用很方便!

    使用HttpClient下载图片

    HttpClient是个很不错的开源框架(org.appache.http),封装了访问http的请求头,参数,内容体,响应等等,使用起来更方面更强大。 HttpURLConnection是java的标准类,可以实现简单的基于URL请求、响应功能,什么都...

    使用 HttpClient 和 HtmlParser 实现简易网络爬虫

    使用 HttpClient 和 HtmlParser 实现简易网络爬虫

    HttpClient的简单使用,get、post、上传、下载

    HttpClient的简单使用,get、post、上传、下载。包含服务端和客户端代码

    HttpClient的简单demo

    这是一个android的HttpClient简单demo,实现了post与get两种方式,包括完整的客户端和服务器端程序,数据库采用的是MySql数据库,数据库部分请用户自己完成,都是最简单的,我已经测试通过,这是我一天的成果,参考...

    httpclient

    使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。 1. 创建HttpClient对象。 2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建...

    httpClient

    HttpClient httpClient = new HttpClient(); // 设置 Http 连接超时为5秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); /* 2 生成 GetMethod 对象并设置参数 */ GetMethod ...

    HttpClientUtil.java

    java httpclient 进行的简单封装,方便大家对httpclient使用。

    使用 HttpClient 和 HtmlParser 实现简易爬虫

    使用 HttpClient 和 HtmlParser 实现简易爬虫

    jsoup+httpclient j简单爬虫

    jsoup+httpclient 简单爬虫,一个jsoup的简单爬虫实例

    httpclient入门

    httpclient入门非常好的资料,新手必备。

    httpclient 最简单 最详细 最入门 教程

    httpclient 最简单 最详细 最入门 教程

Global site tag (gtag.js) - Google Analytics