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.InputStreamReader;
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.GeneralSecurityException;
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.List;
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; }

 


免責聲明!

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



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