封装java-get-post请求方式

package com.ecar.eoc.content.platform.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ecar.commons.cmf.util.PropertiesUtil;
import com.ecar.rtplt.component.web.HttpRequester;
import com.ecar.rtplt.component.web.HttpResponser;

public class RestUtil
{
private static Logger logger = Logger.getLogger(RestUtil.class);
/**
*
* @Title: getRestContent
* @Description: TODO(获取rest返回的内容)
* @param url
* @param map
* @return
* @throws IOException
* @author yuqidi
* @date 2015-4-27 下午07:30:03
* @version V1.0
*/
public static String getRestContent(String url, Map<String, String> map) throws IOException
{
HttpRequester request = new HttpRequester();
request.setDefaultContentEncoding("utf-8");
Map<String, String> properties = new HashMap<String, String>();
properties.put("Content-type", "application/x-www-form-urlencoded; charset=utf-8");

if (logger.isInfoEnabled())
{
logger.info(new StringBuffer("url=").append(url).append(new org.json.JSONObject(map)));
}

HttpResponser responser = request.sendPost(url, map, properties);

return responser.getContent();
}
/**
* base 64 MD5 加密算法
* @Title: judgeToken
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param map
* @param: @param appKey
* @param: @param nonce
* @param: @param timestamp
* @param: @param sign
* @param: @return
* @return: Boolean
* @throws
* @author ecar
* @Date 2016-5-20 上午11:10:46
*/
public static Boolean judgeToken(Map<Object, Object> map, String appKey, String nonce, Long timestamp, String sign)
{

String appSrect = PropertiesUtil.getValueString("ep023_SECRET_KEY", "D90C8B17FB4FBD6442FDEC51C735E91C");
return AuthSigHelpter.authSig(appKey, appSrect, timestamp, nonce, map, sign);
}

/**
* get请求
* @Title: getRestContent_get
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param url
* @param: @param map
* @param: @return
* @param: @throws IOException
* @return: String
* @throws
* @author ecar
* @Date 2016-7-11 下午07:32:02
*/
public static String getRestContent_get(String url) throws IOException
{
HttpRequester request = new HttpRequester();
request.setDefaultContentEncoding("utf-8");
Map<String, String> properties = new HashMap<String, String>();
properties.put("Content-type", "application/x-www-form-urlencoded; charset=utf-8");

if (logger.isInfoEnabled())
{
logger.info(new StringBuffer("url=").append(url));
}

HttpResponser responser = request.sendGet(url);

return responser.getContent();
}
}

原文地址:https://www.cnblogs.com/liuying1995/p/5729164.html