亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這里的每一句都是什么意思

這里的每一句都是什么意思

阿晨1998 2018-12-06 20:21:25
public static void main(String[] args) throws Exception {??????? BasicCookieStore cookieStore = new BasicCookieStore();??????? CloseableHttpClient httpclient = HttpClients.custom()??????????????? .setDefaultCookieStore(cookieStore)??????????????? .build();??????? try {??????????? HttpGet httpget = new HttpGet("https://someportal/");??????????? CloseableHttpResponse response1 = httpclient.execute(httpget);??????????? try {??????????????? HttpEntity entity = response1.getEntity();??????????????? System.out.println("Login form get: " + response1.getStatusLine());??????????????? EntityUtils.consume(entity);??????????????? System.out.println("Initial set of cookies:");??????????????? List<Cookie> cookies = cookieStore.getCookies();??????????????? if (cookies.isEmpty()) {??????????????????? System.out.println("None");??????????????? } else {??????????????????? for (int i = 0; i < cookies.size(); i++) {??????????????????????? System.out.println("- " + cookies.get(i).toString());??????????????????? }??????????????? }??????????? } finally {??????????????? response1.close();??????????? }??????????? HttpUriRequest login = RequestBuilder.post()??????????????????? .setUri(new URI("https://someportal/"))??????????????????? .addParameter("IDToken1", "username")??????????????????? .addParameter("IDToken2", "password")??????????????????? .build();??????????? CloseableHttpResponse response2 = httpclient.execute(login);??????????? try {??????????????? HttpEntity entity = response2.getEntity();??????????????? System.out.println("Login form get: " + response2.getStatusLine());??????????????? EntityUtils.consume(entity);??????????????? System.out.println("Post logon cookies:");??????????????? List<Cookie> cookies = cookieStore.getCookies();??????????????? if (cookies.isEmpty()) {??????????????????? System.out.println("None");??????????????? } else {??????????????????? for (int i = 0; i < cookies.size(); i++) {??????????????????????? System.out.println("- " + cookies.get(i).toString());??????????????????? }??????????????? }??????????? } finally {??????????????? response2.close();??????????? }??????? } finally {??????????? httpclient.close();??????? }??? }
查看完整描述

5 回答

?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

// 使用HttpClient發送請求、接收響應很簡單,一般需要如下幾步即可。
// 1. 創建HttpClient對象。
// 2. 創建請求方法的實例,并指定請求URL。如果需要發送GET請求,創建HttpGet對象;如果需要發送POST請求,創建HttpPost對象。
// 3. 如果需要發送請求參數,可調用HttpGet、HttpPost共同的setParams(HetpParams params)方法來添加請求參數;對于HttpPost對象而言,也可調用setEntity(HttpEntity entity)方法來設置請求參數。
// 4. 調用HttpClient對象的execute(HttpUriRequest request)發送請求,該方法返回一個HttpResponse。
// 5. 調用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可獲取服務器的響應頭;調用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務器的響應內容。程序可通過該對象獲取服務器的響應內容。
// 6. 釋放連接。無論執行方法是否成功,都必須釋放連接
//入口函數
public static void main(String[] args) throws Exception {
??????? //cookie存儲類
??????? BasicCookieStore cookieStore = new BasicCookieStore();
??????? //創建一個httpclient,并設置默認cookie存儲cookieStore
??????? CloseableHttpClient httpclient = HttpClients.custom()
??????????????? .setDefaultCookieStore(cookieStore)
??????????????? .build();
??????? try {
??????????? //創建HttpGet對象,需要發送GET請求https://someportal/
??????????? HttpGet httpget = new HttpGet("https://someportal/");
??????????? //HttpClient對象的execute(HttpUriRequest request)發送請求
??????????? CloseableHttpResponse response1 = httpclient.execute(httpget);
??????????? //try開始,試圖執行
??????????? try {
??????????????? //獲取服務器返回的HttpEntity對象
??????????????? HttpEntity entity = response1.getEntity();
??????????????? //打印返回信息,response.getStatusLine().getStatusCode()==200鏈接成功,==500失敗。
??????????????? System.out.println("Login form get: " + response1.getStatusLine());
??????????????? //HttpEntity對象提供的靜態幫助類,EntityUtils.consume關閉資源
??????????????? EntityUtils.consume(entity);
??????????????? //打印Initial set of cookies:
??????????????? System.out.println("Initial set of cookies:");
??????????????? //獲取Cookies并循環,空的輸出None,否則輸出cookie
??????????????? List<Cookie> cookies = cookieStore.getCookies();
??????????????? if (cookies.isEmpty()) {
??????????????????? System.out.println("None");
??????????????? } else {
??????????????????? for (int i = 0; i < cookies.size(); i++) {
??????????????????????? System.out.println("- " + cookies.get(i).toString());
??????????????????? }
??????????????? }
??????????? } finally {
??????????????? //關閉
??????????????? response1.close();
??????????? }
??????????? //POST請求,并設置URI和參數
??????????? HttpUriRequest login = RequestBuilder.post()
??????????????????? .setUri(new URI("https://someportal/"))
??????????????????? .addParameter("IDToken1", "username")
??????????????????? .addParameter("IDToken2", "password")
??????????????????? .build();
??????????? //執行POST請求
??????????? CloseableHttpResponse response2 = httpclient.execute(login);
??????????? try {
??????????????? //獲取實體對象
??????????????? HttpEntity entity = response2.getEntity();
??????????????? //打印服務器返回信息
??????????????? System.out.println("Login form get: " + response2.getStatusLine());
??????????????? //關閉流
??????????????? EntityUtils.consume(entity);
??????????????? //打印cookie
??????????????? System.out.println("Post logon cookies:");
??????????????? List<Cookie> cookies = cookieStore.getCookies();
??????????????? if (cookies.isEmpty()) {
??????????????????? System.out.println("None");
??????????????? } else {
??????????????????? for (int i = 0; i < cookies.size(); i++) {
??????????????????????? System.out.println("- " + cookies.get(i).toString());
??????????????????? }
??????????????? }
??????????? } finally {
??????????????? response2.close();
??????????? }
??????? } finally {
??????????? //關閉httpclient
??????????? httpclient.close();
??????? }
??? }

查看完整回答
反對 回復 2018-12-16
?
慕后森

TA貢獻1802條經驗 獲得超5個贊

。。。。。。

查看完整回答
反對 回復 2018-12-16
?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

public static void main(String [] args){拋出異常
basiccookiestore =新basiccookiestore()存儲機制;
closeablehttpclient HttpClient = httpclients custom()。
setdefaultcookiestore(存儲機制)。
build();
嘗試{
HttpGet HttpGet =新HttpGet(“https:/ / someportal /”);
closeablehttpresponse response1 = HttpClient執行(HTTPGET);
嘗試{
HttpEntity實體= response1 getentity();
系統。了。println(“登錄形式得到:“+ response1 getstatusline()。);
entityutils消耗(實體);
系統。了。println(“初始設定的餅干:“);
列表<餅干>餅干= getcookies()存儲機制;
如果(餅干。isempty()){
系統。了println(“無”);
{ }
為(int i = 0;i <餅干。size();i++){
系統。了。println(“-”+餅干。讓(我)。tostring());
}
}
最后{
response1();
}
httpurirequest登錄= requestbuilder post()。
seturi。(新的URI(“http:/ / someportal /”))
addparameter。(“idtoken1”,“用戶名”)
addparameter。(“idtoken2”,“密碼”)
build();
closeablehttpresponse反應= HttpClient執行(登錄);
嘗試{
HttpEntity實體=反應。getentity();
系統。了。println(“登錄形式得到:“+反應。getstatusline());
entityutils消耗(實體);
系統。了。println(“后登錄餅干:”);
列表<餅干>餅干= getcookies()存儲機制;
如果(餅干。isempty()){
系統。了println(“無”);
{ }
為(int i = 0;i <餅干。size();i++){
系統。了。println(“-”+餅干。讓(我)。tostring());
}
}
最后{
反應。();
}
最后{
HttpClient();
}
}

查看完整回答
反對 回復 2018-12-16
?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

你這個百度翻譯啊。。。。。

查看完整回答
反對 回復 2018-12-16
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

@狗霸人間: 嗯嗯!

查看完整回答
反對 回復 2018-12-16
  • 5 回答
  • 0 關注
  • 702 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號