httpcomponents-client-4.5.3 学习笔记(1) 1.1 发送请求
来源:互联网 发布:通信算法工程师面试 编辑:程序博客网 时间:2024/06/05 02:42
1.1 发送请求
基本的发送请求例子如下:
CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpget = new HttpGet("http://localhost/");CloseableHttpResponse response = httpclient.execute(httpget);try { <...>} finally { response.close();}
新建一个HTTPGET请求:
HttpGet httpget = new HttpGet( "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");其中的URI可以用URIBuider来构建:
URI uri = new URIBuilder() .setScheme("http") .setHost("www.google.com") .setPath("/search") .setParameter("q", "httpclient") .setParameter("btnG", "Google Search") .setParameter("aq", "f") .setParameter("oq", "") .build();HttpGet httpget = new HttpGet(uri);System.out.println(httpget.getURI());服务器响应信息的使用方法:
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");System.out.println(response.getProtocolVersion());System.out.println(response.getStatusLine().getStatusCode());System.out.println(response.getStatusLine().getReasonPhrase());System.out.println(response.getStatusLine().toString());打印效果:
HTTP/1.1200OKHTTP/1.1 200 OK
遍历响应头部的方法:
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost");response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\"");HeaderIterator it = response.headerIterator("Set-Cookie");while (it.hasNext()) { System.out.println(it.next());}打印效果:
Set-Cookie: c1=a; path=/; domain=localhostSet-Cookie: c2=b; path="/", c3=c; domain="localhost"
这里还提供了遍历响应头部的另一种方法:
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost");response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\"");HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator("Set-Cookie"));while (it.hasNext()) { HeaderElement elem = it.nextElement(); System.out.println(elem.getName() + " = " + elem.getValue()); NameValuePair[] params = elem.getParameters(); for (int i = 0; i < params.length; i++) { System.out.println(" " + params[i]); }}
PS:我认为原始的方法会更实用一点。
HTTP entity
HTTP entity 是保存能重复使用的请求或响应传递的信息。
以下是StringEntity的使用例子:
StringEntity myEntity = new StringEntity("important message", ContentType.create("text/plain", "UTF-8"));System.out.println(myEntity.getContentType());System.out.println(myEntity.getContentLength());System.out.println(EntityUtils.toString(myEntity));System.out.println(EntityUtils.toByteArray(myEntity).length);打印效果:
Content-Type: text/plain; charset=utf-817important message17
HttpEntity 的使用例子:
CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpget = new HttpGet("http://localhost/");CloseableHttpResponse response = httpclient.execute(httpget);try { HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); try { // do something useful } finally { instream.close(); } }} finally { response.close();}
从HttpEntity获取网页信息:
CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpget = new HttpGet("http://localhost/");CloseableHttpResponse response = httpclient.execute(httpget);try { HttpEntity entity = response.getEntity(); if (entity != null) { long len = entity.getContentLength(); if (len != -1 && len < 2048) { System.out.println(EntityUtils.toString(entity,"utf-8"));//utf-8可加可不加 } else { // Stream content out } }} finally { response.close();}
有时候HttpEntity要重复使用多次,你可以用BufferedHttpEntity把HttpEntity保存下来:
CloseableHttpResponse response = <...>HttpEntity entity = response.getEntity();if (entity != null) { entity = new BufferedHttpEntity(entity);}
把文件发送post请求给服务器的方法:
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);
提交HTML表单的方法:
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);
你可以给服务器提供客户端推荐的传输编码,通过StringEntity的setChunked(true)的方法:
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);
你可以通过Response handlers来处理服务器的响应:
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
- httpcomponents-client-4.5.3 学习笔记(1) 1.1 发送请求
- httpcomponents-client-4.5.3 学习笔记(2) 1.2 HttpClient接口
- httpcomponents之httpclient发送http请求
- HttpClient4.X(httpcomponents)学习笔记
- Apache HttpComponents学习笔记(一):请求对象和响应对象
- HttpClient发送http请求(apache httpcomponents包)
- httpcomponents-client 使用小结
- apache- httpcomponents-core4.4.6 学习笔记
- apache httpcomponents-client4.5.3 学习笔记
- HttpComponents(Apache HttpComponents Client 4.1.3)通过代理访问网页的设置方法
- HttpComponents(Apache HttpComponents Client 4.1.3)通过Post方式访问网页或传输参数
- httpcomponents-client-4.3.5实现http的post请求和get请求方法
- httpcomponents https请求实例
- AJAX 学习笔记(7) 发送请求参数
- Apache HttpComponents学习笔记(二):HttpClient 接口
- Apache HttpComponents学习笔记(三):配置HttpClient
- Apache HttpComponents学习笔记(四):HttpClient里的HttpRoute
- Apache HttpComponents学习笔记(四):连接和打开连接
- JVM(8):JVM知识点总览-高级Java工程师面试必备
- 临时手记
- Angular学习随笔(持续更新)
- yarn vs npm
- hdu 5776 抽屉定理
- httpcomponents-client-4.5.3 学习笔记(1) 1.1 发送请求
- Json 解析
- IO流
- 安卓工程依赖方式:Implementation vs API dependency
- window上安装配置node.js
- 欢迎使用CSDN-markdown编辑器
- String intern 方法解析
- jsp 萌新练习
- hadoop常用web功能