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(); 
} 

} 

}
0 0
原创粉丝点击