zoukankan      html  css  js  c++  java
  • 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; }
    人这辈子没法做太多事情,所以每做一件事都要做到精彩绝伦。 因为,这就是我的宿命。人生苦短,你明白吗? 所以这是我为人生做出的选择
  • 相关阅读:
    mssql分页原理及效率分析
    [ActiveX]使用VS2010创建MFC ActiveX工程项目
    Study notes for Discrete Probability Distribution
    oracle存储过程异常捕获
    (Python学习9)Python虚拟机中的一般表达式
    用Arduino做一个可视化网络威胁级别指示器!
    iOS上线项目源码分享
    android实习程序6——拨号通话
    评价等级使用的五星选择,包含半星的选择
    AJAX实现无刷新验证用户名
  • 原文地址:https://www.cnblogs.com/junjun1578/p/13602318.html
Copyright © 2011-2022 走看看