转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/54375734
本文出自【我是干勾鱼的博客】
之前在文章《使用[阿里云](https://l.gushuji.site/aliyun)的ip地址查询服务-购买ip地址查询服务》中讲述了购买阿里云的ip地址查询服务,购买之后就可以使用了。阿里云提供了很多种调用方式,如图:
能够看到所有调用方式都列到这里了。我们点击java,会列出java的调用代码,如图:
内容如下:
public static void main(String[] args) {
String host = "http://jisuip.market.alicloudapi.com";
String path = "/ip/location";
String method = "GET";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE 你自己的AppCode");
Map<String, String> querys = new HashMap<String, String>();
querys.put("ip", "122.224.186.100");
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();
}
}
这是java下的调用示例。怎么说呢,我觉得阿里云在这里还是欠点火候呀,说的太简单了,我这里给大家详细介绍一下。
从上面的注释里,能看出这应该是依赖于一个aliyun保存在github上的一个maven项目:
这里我们需要做三件事情:
- 下载github上的库
- 将其转换为maven项目
- 测试ip地址查询服务
下载并在eclipse上创建github仓库
在api-gateway-demo-sign-java仓库中,复制仓库地址,如图:
在eclipse中,打开“Git Repositories”视图,上侧有一个小的快捷键,鼠标移上去会显示:
clone a Git Repository and add the clone to this view
如图:
点击这个按钮,如图:
将github仓库地址填好,一般如果刚复制完这里会自动出现,点击next,如图:
直接点击next,如图:
这里选择github项目的本地仓库的存放位置,然后点击Finish,这样就会下载相应的仓库,如图:
将github仓库项目设定为maven项目
这个时候如果你点击“Navigator”视图,与“Project Explorer”视图下都还看不到项目。
在“Git Repository”视图下,右键点击项目,选择“Import Projects”,如图所示:
在弹出框中选择第三个“Import as general project”,如图:
点击next,如图:
点击Finish。这个时候,在“Navigator”视图下能看到这个项目,如图:
在“Project Explorer”视图下也能看到这个项目,如图:
文件pom.xml也在里面。
现在我们在“Navigator”视图下,右键点击项目:
Configure -> convert to maven project
这时候,“Navigator”视图为:
“Project Explorer”视图为:
测试ip地址查询服务
这时候在“Project Explorer”视图下源代码状态都生产了。现在我们需要在package:
com.aliyun.api.gateway.demo
下创建一个类Test,内容就是将本文最开始提到的“java请求示例”填进去。内容如下:
package com.aliyun.api.gateway.demo;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import com.aliyun.api.gateway.demo.util.HttpUtils;
public class Test {
public static void main(String[] args) {
String host = "http://jisuip.market.alicloudapi.com";
String path = "/ip/location";
String method = "GET";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE 你自己的AppCode");
Map<String, String> querys = new HashMap<String, String>();
querys.put("ip", "122.224.186.100");
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();
}
}
}
注意这里我们做了几个修改。
一个是将:
你自己的AppCode
改为了我们自己账号的AppCode。
再有是将:
querys.put(“ip”, “122.224.186.100”);
中的ip改为你要查询的ip值。
还有是将:
//System.out.println(EntityUtils.toString(response.getEntity()));
这个注释去掉。
执行以下,可以看到控制台成功的结果:
具体的数据就可以通过这个json穿去获取啦。
真正的时候当然还需要将这个工程打包成一个war包来调用。到这里使用java调用ip地址查询服务就讲完啦!