HttpClient4.5 基础教程 执行请求<三>
来源:互联网 发布:小盘科技股 知乎 编辑:程序博客网 时间:2024/06/05 03:12
更多HttpClient4.5中文教程请查看:点击打开链接
===============================================
1.1.7.生产实体内容
HttpClient提供了几个类,用来通过HTTP连接高效地传输内容。这些类的实例均与内含实体请求有关,比如POST和PUT,它们能够把实体内容封装进友好的HTTP请求中。对于基本的数据容器String, byte array, input stream, and file,HttpClient为它们提供了几个对应的类:StringEntity, ByteArrayEntity, InputStreamEntity, and FileEntity。- File file = new File(“somefile.txt”);
- FileEntity entity = new FileEntity(file,ContentType.create(“text/plain”, “UTF-8”));
- HttpPost httppost = new HttpPost(“http://localhost/action.do”);
- httppost.setEntity(entity);
File file = new File("somefile.txt");FileEntity entity = new FileEntity(file,ContentType.create("text/plain", "UTF-8"));HttpPost httppost = new HttpPost("http://localhost/action.do");httppost.setEntity(entity);请注意InputStreamEntity是不可重复的,因为它仅仅能够从数据流中读取一次。一般建议实现一个定制的HttpEntity类来代替使用一般的InputStreamEntity。FileEntity将会是一个好的出发点。
1.1.7.1 HTML表单
许多应用需要模仿一个登陆HTML表单的过程,比如:为了登陆一个web应用或者提交输入的数据。HttpClient提供了UrlEncodedFormEntity类来简化这个过程。- List<NameValuePair> formparams = new ArrayList<NameValuePair>();
- formparams.add(new BasicNameValuePair(“param1”, “value1”));
- formparams.add(new BasicNameValuePair(“param2”, “value2”));
- UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
- Consts.UTF_8);
- HttpPost httppost = new HttpPost(“http://localhost/handler.do”);
- httppost.setEntity(entity);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();formparams.add(new BasicNameValuePair("param1", "value1"));formparams.add(new BasicNameValuePair("param2", "value2"));UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);HttpPost httppost = new HttpPost("http://localhost/handler.do");httppost.setEntity(entity);UrlEncodedFormEntity实例像上面一样使用URL编码方式来编码参数并生成下面的内容:
param1=value1¶m2=value2
1.1.7.2 内容分块
通常,我们推荐让HttpClient选择基于被传递的HTTP报文属相最合适的传输编码方式。可能地,可以通过设置HttpEntity#setChunked()为true来通知HttpClient你要进行分块编码。注意HttpClient将会使用这个标志作为提示。当使用一些不支持分块编码的HTTP版本(比如HTTP/1.0.)时,这个值将会忽略。【译者:分块编码是是HTTP1.1协议中定义的Web用户向服务器提交数据的一种方法】
- StringEntity entity = new StringEntity(“important message”,
- ContentType.create(”plain/text”, Consts.UTF_8));
- entity.setChunked(true);
- HttpPost httppost = new HttpPost(“http://localhost/acrtion.do”);
- httppost.setEntity(entity);
StringEntity entity = new StringEntity("important message", ContentType.create("plain/text", Consts.UTF_8));entity.setChunked(true);HttpPost httppost = new HttpPost("http://localhost/acrtion.do");httppost.setEntity(entity);
1.1.8.响应处理器
最简单、最方便的方式来处理响应是使用ResponseHandler接口,它包含了一个handleResponse(HttpResponse response)方法。这个方法减轻使用者对于连接管理的担心。当你使用ResponseHandler时,无论是请求执行成功亦或出现异常,HttpClient将会自动地确保连接释放回连接管理器中。
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpGet httpget = new HttpGet(“http://localhost/json”);
- ResponseHandler<MyJsonObject> rh = new ResponseHandler<MyJsonObject>() {
- @Override
- public JsonObject handleResponse(final HttpResponse response) throws IOException {
- StatusLine statusLine = response.getStatusLine();
- HttpEntity entity = response.getEntity();
- if (statusLine.getStatusCode() >= 300) {
- throw new HttpResponseException(statusLine.getStatusCode(),
- statusLine.getReasonPhrase());
- }
- if (entity == null) {
- throw new ClientProtocolException(“Response contains no content”);
- }
- Gson gson = new GsonBuilder().create();
- ContentType contentType = ContentType.getOrDefault(entity);
- Charset charset = contentType.getCharset();
- Reader reader = new InputStreamReader(entity.getContent(), charset);
- return gson.fromJson(reader, MyJsonObject.class);
- }
- };
- MyJsonObject myjson = client.execute(httpget, rh);
CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpget = new HttpGet("http://localhost/json");ResponseHandler<MyJsonObject> rh = new ResponseHandler<MyJsonObject>() { @Override public JsonObject handleResponse(final HttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (entity == null) { throw new ClientProtocolException("Response contains no content"); } Gson gson = new GsonBuilder().create(); ContentType contentType = ContentType.getOrDefault(entity); Charset charset = contentType.getCharset(); Reader reader = new InputStreamReader(entity.getContent(), charset); return gson.fromJson(reader, MyJsonObject.class); }};MyJsonObject myjson = client.execute(httpget, rh);
阅读全文
0 0
- HttpClient4.5 基础教程 执行请求<三>
- httpclient4.5 入门教程 1.1执行请求
- httpclient4.5 入门教程 1.1执行请求<二>
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(三)
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(三)
- HttpClient4.5发送post请求
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(一)
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(二)
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(一)
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(二)
- Apache HttpClient4.5(三)
- 使用HttpClient4.5 PoolingHttpClientConnectionManager 提交Http请求
- HttpClient4.5 post请求xml到服务器
- httpclient4.x代理请求
- HttpClient4.x 多线程请求
- HttpClient4.3.3 https请求
- 【HttpClient4.5实训】一.HttpClient4.5模拟浏览器GET请求访问新浪网(非原文教程)
- 【HttpClient4.5实训】一.HttpClient4.5模拟浏览器GET请求访问新浪网(非原文教程)
- 51Nod-1530-稳定方块
- js中的通用事件
- BZOJ 1552: [Cerc2007]robotic sort
- HDU1232 畅通工程 并查集入门
- nginx的安装和配置
- HttpClient4.5 基础教程 执行请求<三>
- leetCode 41.First Missing Positive (第一个丢掉的正数) 解题思路和方法
- B
- HDU6047优先队列+贪心
- 【ML--06】主成分分析PCA
- win10 激活
- XML转Map集合以及对象化XML
- 关于display用法以及作用
- 二维指针、数组指针和指针数组存储空间的动态分配和释放