HttpClient学习--HttpClient的POST请求过程源码解读

众所周知,HttpClient是对JDK net包下网络相关操作的一个封装,所以阅读的前提待知道HttpClient底层肯定是通过Socket来进行网络通信的。

下面来简单的捋一下代码,在进入繁杂、深层的代码之前待提醒自己保持清醒,不能由于一层一层的引导迷茫了思绪。并且要保持疑问和警惕,否则可能就会无功而返。

 明确目标,只是捋代码,并不是把整个结构都捋清,知道哪些类是干什么的,扮演着什么样的角色即可。

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://httpbin.org/post");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
HttpEntity entity2 = response2.getEntity();
System.out.println(EntityUtils.toString(entity2));
response2.close();
httpclient.close();

上面是官网上的POST请求的示例。
一行一行的捋

猜你喜欢

转载自www.cnblogs.com/caiyao/p/9215244.html