依賴於Apache模擬Http請求


一、依賴的jar包

httpclient-4.5.jar
httpclient-cache-4.5.jar
httpcore-4.4.1.jar

package com.lava.lavafaq.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/*
 * 依賴於Apache庫
 */
public class HttpRequest {  
    
    public static String get(String url)
    { 
        String result="";
        CloseableHttpClient client = HttpClients.createDefault();
        try
        {
             HttpGet httpGet = new HttpGet(url);
             CloseableHttpResponse resp = client.execute(httpGet);
             try
             {
                 HttpEntity entity = resp.getEntity();
                 if (null != entity)
                 {                    
                     result=EntityUtils.toString(entity);
                 }
             }
             catch(Exception e)
             {
                 
             }
             finally
             {
                 resp.close();
             }
        }
        catch(Exception e)
        {
            
        }
        finally
        {
            try {
                client.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return result;
    }
    
    
    public static String post(String url,List<NameValuePair> paramPairs)
    {
        String result="";
         // 1. 獲取默認的client實例
        CloseableHttpClient client = HttpClients.createDefault();
        // 2. 創建httppost實例
        HttpPost httpPost = new HttpPost(url);
        // 3. 創建參數隊列(鍵值對列表)
//        List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
//         paramPairs.add(new BasicNameValuePair("username", "admin"));
//         paramPairs.add(new BasicNameValuePair("password", "123456"));
         UrlEncodedFormEntity entity;
         try
         {
            // 4. 將參數設置到entity對象中
             entity = new UrlEncodedFormEntity(paramPairs, "UTF-8");
             // 5. 將entity對象設置到httppost對象中
             httpPost.setEntity(entity);
             System.out.println("executing POST request " + httpPost.getURI());
            // 6. 發送請求並回去響應
             CloseableHttpResponse resp = client.execute(httpPost);
             try
             {
                  HttpEntity respEntity = resp.getEntity();
                  if (null != respEntity) 
                  {
                      result=EntityUtils.toString(respEntity, "UTF-8");                      
                  }                 
                 
             }
             catch(Exception e)
             {

             }
             finally
             { 
                 // 9. 關閉響應對象
                 resp.close();
             }
         }
         catch(Exception e)
         {
             
         }
         finally
         {
            // 10. 關閉連接,釋放資源
             try {
                client.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         }
         return result;
    }
}

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM