HttpClient學習--HttpClient的POST請求過程源碼解讀
阿新 • • 發佈:2018-06-22
源碼解讀 pri enc sock lis post 通信 bsp system
眾所周知,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請求的示例。
一行一行的捋
HttpClient學習--HttpClient的POST請求過程源碼解讀