restful接口的调用
来源:互联网 发布:手机阿里云域名修改dns 编辑:程序博客网 时间:2024/06/05 09:26
1.get方式
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
public class MapService implements IMapService {
//日志记录
private static final Logger LOG = LoggerFactory.getLogger(MapService.class);
//FOSS获取长地址url
private String shortAdrUrl;
public String getShortAdrUrl() {
return shortAdrUrl;
}
public void setShortAdrUrl(String shortAdrUrl) {
this.shortAdrUrl = shortAdrUrl;
}
/**
*
* @作者:fei
* @功能:获取短地址对应的长地址
* @时间:2014年11月5日下午8:14:18
* @param:
* @return:
*/
@Override
public String transAdr(String shortUrl) {
String urlPath = shortAdrUrl + shortUrl;
String longUrl = "";
URL url;
try {
url = new URL(urlPath);
HttpURLConnection httpConn = ((HttpURLConnection) url.openConnection());
httpConn.addRequestProperty("ContenType","application/json;charset=utf-8");
InputStream input = httpConn.getInputStream();
String jsonString = IOUtils.toString(input);
JSONObject jsonObj = JSONObject.parseObject(jsonString);
//获取的状态, 成功为true,失败为false
JSONObject jsonObj2 = jsonObj.getJSONObject("OrgGisUrlInfoDto");
boolean state = jsonObj2.getBoolean("state");
//成功获取长地址
if(state) {
longUrl = jsonObj2.get("depCoodinate").toString();
LOG.info("短地址信息为:" + shortUrl + ",对应的长地址信息为:" + longUrl);
}
} catch (MalformedURLException e) {
e.printStackTrace();
LOG.error(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
LOG.error(e.getMessage());
}
return longUrl;
}
}
-------------------------------------------------------
2、post方式
import java.io.IOException;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.codehaus.jackson.map.ObjectMapper;
import com.alibaba.fastjson.JSONObject;
public class TestFossUrl {
/**
* MAP类型数组转换成NameValuePair类型
*
* @param properties
* MAP类型数组
* @return NameValuePair类型数组
* http://192.168.17.141:8680/esb2/rs/ESB_CRM2ESB_ORDER_STATE"
* http://10.224.65.123:8089/webWsPro/webservice/ows/app/changed;
*/
private static final String URI = "http://10.224.70.124:8080/bse-baseinfo-web/services/gisUrl/queryLongUrlByShortUrl";
public static void main(String[] args) throws IOException {
HttpClient httpClient = new HttpClient();
// 设置编码格式
httpClient.getParams().setContentCharset("UTF-8");
// 构造PostMethod的实例
PostMethod postMethod = new PostMethod(URI);
try {
JSONObject jsonObj = new JSONObject();
jsonObj.put("shortUrl", "llllll");
String js = jsonObj.toString();
RequestEntity entity = new StringRequestEntity(js,
"application/json", "UTF-8");
postMethod.setRequestEntity(entity);
postMethod.addRequestHeader("Content-Type",
"application/json;charset=UTF-8");
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != 200) {
// LOGGER.info("ESBADDRESS_REQUEST_ERROR_URL_CANNOTFOUND"+propertyFactory.getEsbRsUrl());
}
String responseBody = postMethod.getResponseBodyAsString();
Map<String, Object> returnMap = new ObjectMapper().readValue(
responseBody, Map.class);
System.out.println(responseBody);
} catch (Exception e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
}
}
import java.io.IOException;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.codehaus.jackson.map.ObjectMapper;
import com.alibaba.fastjson.JSONObject;
public class TestFossUrl {
/**
* MAP类型数组转换成NameValuePair类型
*
* @param properties
* MAP类型数组
* @return NameValuePair类型数组
* http://192.168.17.141:8680/esb2/rs/ESB_CRM2ESB_ORDER_STATE"
* http://10.224.65.123:8089/webWsPro/webservice/ows/app/changed;
*/
private static final String URI = "http://10.224.70.124:8080/bse-baseinfo-web/services/gisUrl/queryLongUrlByShortUrl";
public static void main(String[] args) throws IOException {
HttpClient httpClient = new HttpClient();
// 设置编码格式
httpClient.getParams().setContentCharset("UTF-8");
// 构造PostMethod的实例
PostMethod postMethod = new PostMethod(URI);
try {
JSONObject jsonObj = new JSONObject();
jsonObj.put("shortUrl", "llllll");
String js = jsonObj.toString();
RequestEntity entity = new StringRequestEntity(js,
"application/json", "UTF-8");
postMethod.setRequestEntity(entity);
postMethod.addRequestHeader("Content-Type",
"application/json;charset=UTF-8");
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != 200) {
// LOGGER.info("ESBADDRESS_REQUEST_ERROR_URL_CANNOTFOUND"+propertyFactory.getEsbRsUrl());
}
String responseBody = postMethod.getResponseBodyAsString();
Map<String, Object> returnMap = new ObjectMapper().readValue(
responseBody, Map.class);
System.out.println(responseBody);
} catch (Exception e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
}
}
0 0
- restful接口的调用
- 基于Restful接口的网络API接口调用方法
- delphi7调用JAVA RESTFUL接口
- java客户端调用restful接口
- java客户端调用restful接口
- Jersey调用Webservice Restful接口
- java客户端调用restful接口
- Azkaban的AJAX的RestFul接口调用事例
- Azkaban的AJAX的RestFul接口调用事例
- yii2-restful的接口优化和实现互相调用
- ASP如何调用Delphi开发的DataSnap/Restful接口服务
- PHP如何调用Delphi开发的DataSnap/Restful接口服务
- Java 调用Restful API接口的几种方式--HTTPS
- 调用Metasploit RestFul接口,解决证书问题
- 三种方法实现调用Restful接口
- java实现客户端Client调用Restful接口
- springMVC的Restful接口实现
- RESTful的接口测试-RESTClient
- 拦截结果监听器
- hdu 1254 推箱子(BFS)
- SVN版本管理工具
- 常见HTTP状态
- webbench-1.5源代码分析笔记
- restful接口的调用
- Junit4单元测试(1)
- linux下热插拔事件的产生是怎样通知到用户空间,kobject_uevent_env之uevent
- GBDT(MART) 迭代决策树入门教程 | 简介
- SEAndroid策略分析
- poj3669
- java对properties配置文件的读写操作
- Ubuntu中Samba共享工具安装及配置
- C++:操作单向链表的实现