http請求介面呼叫
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Random;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.alibaba.dubbo.common.json.ParseException;
import com.alibaba.fastjson.JSON;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectResult;
import com.ezhiyang.account.common.result.ReturnS;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Encoder;
@Test public void postParams() { // 獲取連線客戶端工具 CloseableHttpClient httpClient = HttpClients.createDefault(); String entityStr = null; CloseableHttpResponse response = null; try { // 建立POST請求物件 HttpPost httpPost = new HttpPost("http://www.baidu.com"); /* * 新增請求引數 */ // 建立請求引數 List<NameValuePair> list = new LinkedList<>(); BasicNameValuePair param1 = new BasicNameValuePair("name", "root"); BasicNameValuePair param2 = new BasicNameValuePair("password", "123456"); list.add(param1); list.add(param2); // 使用URL實體轉換工具 UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8"); httpPost.setEntity(entityParam); /* * 新增請求頭資訊 */ // 瀏覽器表示 httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)"); // 傳輸的型別 httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); // 執行請求 response = httpClient.execute(httpPost); // 獲得響應的實體物件 HttpEntity entity = response.getEntity(); // 使用Apache提供的工具類進行轉換成字串 entityStr = EntityUtils.toString(entity, "UTF-8"); // System.out.println(Arrays.toString(response.getAllHeaders())); } catch (ClientProtocolException e) { System.err.println("Http協議出現問題"); e.printStackTrace(); } catch (ParseException e) { System.err.println("解析錯誤"); e.printStackTrace(); } catch (IOException e) { System.err.println("IO異常"); e.printStackTrace(); } finally { // 釋放連線 if (null != response) { try { response.close(); httpClient.close(); } catch (IOException e) { System.err.println("釋放連接出錯"); e.printStackTrace(); } } } // 列印響應內容 System.out.println(entityStr);
return entityStr; } @Test public void getParams() { // 獲取連線客戶端工具 CloseableHttpClient httpClient = HttpClients.createDefault(); String entityStr = null; CloseableHttpResponse response = null; try { /* * 由於GET請求的引數都是拼裝在URL地址後方,所以我們要構建一個URL,帶引數 */ URIBuilder uriBuilder = new URIBuilder("http://www.baidu.com"); /** 第一種新增引數的形式 */ /*uriBuilder.addParameter("name", "root"); uriBuilder.addParameter("password", "123456");*/ /** 第二種新增引數的形式 */ List<NameValuePair> list = new LinkedList<>(); BasicNameValuePair param1 = new BasicNameValuePair("name", "root"); BasicNameValuePair param2 = new BasicNameValuePair("password", "123456"); list.add(param1); list.add(param2); uriBuilder.setParameters(list); // 根據帶引數的URI物件構建GET請求物件 HttpGet httpGet = new HttpGet(uriBuilder.build()); /* * 新增請求頭資訊 */ // 瀏覽器表示 httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)"); // 傳輸的型別 httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded"); // 執行請求 response = httpClient.execute(httpGet); // 獲得響應的實體物件 HttpEntity entity = response.getEntity(); // 使用Apache提供的工具類進行轉換成字串 entityStr = EntityUtils.toString(entity, "UTF-8"); } catch (ClientProtocolException e) { System.err.println("Http協議出現問題"); e.printStackTrace(); } catch (ParseException e) { System.err.println("解析錯誤"); e.printStackTrace(); } catch (URISyntaxException e) { System.err.println("URI解析異常"); e.printStackTrace(); } catch (IOException e) { System.err.println("IO異常"); e.printStackTrace(); } finally { // 釋放連線 if (null != response) { try { response.close(); httpClient.close(); } catch (IOException e) { System.err.println("釋放連接出錯"); e.printStackTrace(); } } } // 列印響應內容 System.out.println(entityStr); return entityStr; }