阿里云API调用方法

在这里小小推荐下我的个人博客

csdn:雷园的csdn博客

个人博客:雷园的个人博客

简书:雷园的简书

  1. 添加依赖
<!-- commons --><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><!-- api接口 --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.4</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency>
  1. 下载HttpUtils.java,工具类下载
    或者直接copy这段代码:HttpUtils.java
package com.leiyuan.bs.util;import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;public class HttpUtils {/*** get** @param host* @param path* @param method* @param headers* @param querys* @return* @throws Exception*/public static HttpResponse doGet(String host, String path, String method,Map<String, String> headers,Map<String, String> querys)throws Exception {HttpClient httpClient = wrapClient(host);HttpGet request = new HttpGet(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}return httpClient.execute(request);}/*** post form** @param host* @param path* @param method* @param headers* @param querys* @param bodys* @return* @throws Exception*/public static HttpResponse doPost(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,Map<String, String> bodys)throws Exception {HttpClient httpClient = wrapClient(host);HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (bodys != null) {List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();for (String key : bodys.keySet()) {nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));}UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");request.setEntity(formEntity);}return httpClient.execute(request);}/*** Post String** @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPost(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,String body)throws Exception {HttpClient httpClient = wrapClient(host);HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (StringUtils.isNotBlank(body)) {request.setEntity(new StringEntity(body, "utf-8"));}return httpClient.execute(request);}/*** Post stream** @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPost(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,byte[] body)throws Exception {HttpClient httpClient = wrapClient(host);HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (body != null) {request.setEntity(new ByteArrayEntity(body));}return httpClient.execute(request);}/*** Put String** @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPut(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,String body)throws Exception {HttpClient httpClient = wrapClient(host);HttpPut request = new HttpPut(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (StringUtils.isNotBlank(body)) {request.setEntity(new StringEntity(body, "utf-8"));}return httpClient.execute(request);}/*** Put stream** @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPut(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,byte[] body)throws Exception {HttpClient httpClient = wrapClient(host);HttpPut request = new HttpPut(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (body != null) {request.setEntity(new ByteArrayEntity(body));}return httpClient.execute(request);}/*** Delete** @param host* @param path* @param method* @param headers* @param querys* @return* @throws Exception*/public static HttpResponse doDelete(String host, String path, String method,Map<String, String> headers,Map<String, String> querys)throws Exception {HttpClient httpClient = wrapClient(host);HttpDelete request = new HttpDelete(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}return httpClient.execute(request);}private static String buildUrl(String host, String path, Map<String, String> querys) throwsUnsupportedEncodingException {StringBuilder sbUrl = new StringBuilder();sbUrl.append(host);if (!StringUtils.isBlank(path)) {sbUrl.append(path);}if (null != querys) {StringBuilder sbQuery = new StringBuilder();for (Map.Entry<String, String> query : querys.entrySet()) {if (0 < sbQuery.length()) {sbQuery.append("&");}if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {sbQuery.append(query.getValue());}if (!StringUtils.isBlank(query.getKey())) {sbQuery.append(query.getKey());if (!StringUtils.isBlank(query.getValue())) {sbQuery.append("=");sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));}}}if (0 < sbQuery.length()) {sbUrl.append("?").append(sbQuery);}}return sbUrl.toString();}private static HttpClient wrapClient(String host) {HttpClient httpClient = new DefaultHttpClient();if (host.startsWith("https://")) {sslClient(httpClient);}return httpClient;}private static void sslClient(HttpClient httpClient) {try {SSLContext ctx = SSLContext.getInstance("TLS");X509TrustManager tm = new X509TrustManager() {public X509Certificate[] getAcceptedIssuers() {return null;}public void checkClientTrusted(X509Certificate[] xcs, String str) {}public void checkServerTrusted(X509Certificate[] xcs, String str) {}};ctx.init(null, new TrustManager[]{tm}, null);SSLSocketFactory ssf = new SSLSocketFactory(ctx);ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);ClientConnectionManager ccm = httpClient.getConnectionManager();SchemeRegistry registry = ccm.getSchemeRegistry();registry.register(new Scheme("https", 443, ssf));} catch (KeyManagementException ex) {throw new RuntimeException(ex);} catch (NoSuchAlgorithmException ex) {throw new RuntimeException(ex);}}
}
  1. 从API官方文档中查看调用代码,例如
public static void main(String[] args) {String host = "http://ali-news.showapi.com";String path = "/newsList";String method = "GET";String appcode = "你自己的AppCode";Map<String, String> headers = new HashMap<String, String>();//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105headers.put("Authorization", "APPCODE " + appcode);Map<String, String> querys = new HashMap<String, String>();querys.put("channelId", "5572a108b3cdc86cf39001cd");querys.put("channelName", "channelName");querys.put("maxResult", "20");querys.put("needAllList", "1");querys.put("needContent", "0");querys.put("needHtml", "0");querys.put("page", "1");querys.put("title", "title");try {/*** 重要提示如下:* HttpUtils请从* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java* 下载** 相应的依赖请参照* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml*/HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);System.out.println(response.toString());//获取response的body//System.out.println(EntityUtils.toString(response.getEntity()));} catch (Exception e) {e.printStackTrace();}}

EntityUtils.toString(response.getEntity())即为返回数据。

阿里云各种API如何使用相关推荐

  1. 基于阿里云的API简介

    基于阿里云的API简介 API简介 如果您熟悉网络服务协议和一种以上编程语言,推荐您调用API管理您的云上资源和开发自己的应用程序. 使用说明 ECS API支持HTTP或者HTTPS网络请求协议,允 ...

  2. 阿里云调用api配置access_key

    前言 阿里云调用api需要验证accesskey,由于多个脚本需要调用,所以我写在一个单独的脚本中 脚本内容: #!/usr/bin/env python #coding=utf-8from aliy ...

  3. 微信小程序使用阿里云物联网API开发物联网应用

    微信小程序是一种不需要下载安装即可使用的应用,它实现了应用"触手可及"的梦想,用户扫一扫或者搜一下就可以打开的应用. 微信小程序具有方便快捷,速度快,安全及保密性高的优点,同时开发 ...

  4. 阿里云服务器 API 的使用

    阿里云服务器 API 的使用 针对,云服务器的 API 使用 针对于阿里云 会有两个 认证的key AccessKey : ID 和 Secret API 文档 : 给的是接口 实例相关接口 http ...

  5. 调用阿里云web API实现滑块验证码

    文章目录 滑块验证码的实现原理 调用阿里云web API实现图形验证码 效果演示: 本来想着弄一个算术验证码的,后来发现这玩意儿对我自己也太不友好了

  6. ■ 直接调用阿里云视频点播API实现视频播放

    前言:公司最近要实现一个视频播放的功能,正常是不需要移动端调用阿里云视频API的,这件事是由后台来完成的.但是既然需求交给我了,就要想办法完成. 先来看一眼官方的API调用文档 https://hel ...

  7. 踩坑记:C#访问阿里云的API小结,阿里云的文档有待改善……

    为运维管理方便需要,写了一个小工具去调用阿里云的API,包括操作ECS.SLB.域名等等API,结果就这么一点点小东西,也被阿里云的文档坑了好多次,下面5个问题,有3个跟阿里云文档相关-- 关键是阿里 ...

  8. 基于阿里云 DNS API 实现的 DDNS 工具

    0.简要介绍 0.1 思路说明 AliDDNSNet 是基于 .NET Core 开发的动态 DNS 解析工具,借助于阿里云的 DNS API 来实现域名与动态 IP 的绑定功能.工具核心就是调用了阿 ...

  9. API信息全掌控,方便你的日志管理——阿里云推出API网关打通日志服务

    摘要: 近日,阿里云API网关对接了日志服务,可以输出用户在API网关产生的API调用日志,目前支持将 API 接入 API 网关的用户查看日志明细.概况.报表分析.在线查询等. 访问日志(Accce ...

  10. 阿里云启动API创新大赛 设视频技术为场景赛题

    摘要: 阿里云API大赛一直以践行API经济为主旨,涌现出了很多基于API服务的优秀解决方案方案作品.本届API大赛主题为"智慧开放,互链解决",基于广义的API经济理念,将不局限 ...

最新文章

  1. 为什么叫python编程-运维为什么要学编程?编程为什么是Python?
  2. 解决修改“文件夹选项”后仍不能显示隐藏文件一例
  3. [转]java取得Linuxcpu,内存,磁盘实时信息
  4. redis linux安装配置,linux下安装配置单点redis
  5. C#一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第100位数是多少, 用递归算法实现。...
  6. 通达信版弘历软件指标_通达信软件指标编写基础教程,10个指标源码祝你股市一帆风顺...
  7. LeetCode 22. 括号生成(回溯/DP)
  8. new操作符的作用是什么
  9. netapp脚本保存日志_Shell脚本实战:日志关键字监控+自动告警
  10. 中西造园水法浅比【ZZ】
  11. 卷积神经网络训练准确率突然下降_从MobileNet看轻量级神经网络的发展
  12. 【洛谷 1057】传球游戏
  13. SortedSet和TreeSet
  14. mysql提权马免杀_webshell/牛逼免杀提权隐藏大马 (1).asp at master · tennc/webshell · GitHub...
  15. Word2010编号、多级列表、样式、图注的综合设置
  16. 为航空公司注入数字活力,腾讯助力祥鹏航空数字客舱圆满首航
  17. 万物皆为叠加态粒子:如何用量子物理学诠释生活?
  18. 系统重构过程中的异构数据同步回环处理
  19. html5绘制心形图案,HTML5/Canvas 渐变色彩的心形图案
  20. 关于cuda、cudnn环境配置

热门文章

  1. SSD1315驱动的OLED
  2. window 和linux系统分隔符的不同
  3. 健康——每日饮水量建议
  4. 如何删除kafka消费组
  5. maven 本地仓库的配置以及如何修改默认.m2仓库位置
  6. 题目:两道迷宫类型题
  7. 马化腾动怒!微信数据“被共享”,山寨微信团伙被判一年!
  8. python时间序列分析包_python关于时间序列的分析
  9. ASP.NET Web API实现简单的文件下载与上传
  10. 程序员们逢年过节初一十五都应该祭拜哪些神仙?