java+httpclient—-稍微复杂的get请求
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java+httpclient—-稍微复杂的get请求,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2203字,纯文字阅读大概需要4分钟。
内容图文
package jkcs; import java.io.IOException; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class jiekoumoni { public static void main(String[] args) throws ClientProtocolException, IOException { CloseableHttpClient client = HttpClients.createDefault(); //创建一个http客户端 HttpGet httpGet = new HttpGet("http://www.baidu.com"); // 通过httpget方式来实现我们的get请求 CloseableHttpResponse Response = client.execute(httpGet); // 通过client调用execute方法,得到我们的执行结果就是一个response,所有的数据都封装在response里面了 //----------------------------------------------------------------------- System.out.println(Response.getStatusLine()); //HTTP/1.1 200 OK System.out.println(Response.getProtocolVersion()); //HTTP/1.1 System.out.println(Response.getStatusLine().getStatusCode()); //200 //----------------------------------------------------------------------- HttpEntity httpEntity = Response.getEntity(); //获取某个固定的响应头 System.out.println(httpEntity.getContentType()); //Content-Type: text/html //----------------------------------------------------------------------- System.out.println(Response.getFirstHeader("Content-Type"));//Content-Type: text/html System.out.println(Response.getFirstHeader("Date")); //Date: Sun, 03 May 2020 06:58:16 GMT //----------------------------------------------------------------------- Header[] headers = Response.getAllHeaders(); //获取所有响应头for (Header header : headers) { System.out.println("Key : " + header.getName()+", ," +" Value : " + header.getValue()); } //----------------------------------------------------------------------- Response.close(); // 关闭 } }
执行结果:
HTTP/1.1 200 OK
HTTP/1.1
200
Content-Type: text/html
Content-Type: text/html
Date: Sun, 03 May 2020 06:58:16 GMT
Key : Content-Type, , Value : text/html
Key : Server, , Value : bfe
Key : Date, , Value : Sun, 03 May 2020 06:58:16 GMT
原文:https://www.cnblogs.com/xiaobaibailongma/p/12822015.html
内容总结
以上是互联网集市为您收集整理的java+httpclient—-稍微复杂的get请求全部内容,希望文章能够帮你解决java+httpclient—-稍微复杂的get请求所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。