1.
public static String getAccessToken() {
String finalUrl = SERVER_ADDRESS + ACCESS_TOKEN_URI;
Map<String, Object> param = Maps.newHashMap();
param.put( "appKey", APPKEY );
param.put( "appSecret", APPSECRET );
ResponseMessage rspMessage = HttpsUtils.sendRequest( finalUrl, param );
if (rspMessage.getStatusCode() == 200) {
String resultContent = rspMessage.getContent();
// 序列化方法使用Jackson为底包,可自行封装
Map<String, Object> resultMap = JSON.parseObject( resultContent, new TypeReference<Map<String, Object>>() {
} );
// 结果Code为0表示调用成功
if ((Integer) resultMap.get( "code" ) == 0) {
String dataJson = resultMap.get( "data" ).toString();
Map<String, Object> dataMap = (Map<String, Object>) resultMap.get( "data" );
// expriesIn 为Token过期时间,注意缓存Token时以此时间为标准自定义缓存过期时间
Integer expriesIn = (Integer) dataMap.get( "expriesIn" );
// 连接世强服务商接口凭据,凭据包含身份等信息,注意保密,缓存每日获取有次数限制,请注意缓存循环使用。
String accessToken = (String) dataMap.get( "accessToken" );
return accessToken;
}
}
return "";
}
2.
public class HttpsUtils {
private static Logger log = LoggerFactory.getLogger( HttpsUtils.class );
@SuppressWarnings("rawtypes")
public static ResponseMessage sendRequest(String requestUrl, Map<String, Object> requestParamsMap) {
ResponseMessage rspMessage = new ResponseMessage();
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
StringBuffer responseResult = new StringBuffer();
StringBuffer params = new StringBuffer();
HttpURLConnection httpURLConnection = null;
try {
// 组织请求参数
Iterator it = requestParamsMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry element = (Map.Entry) it.next();
params.append( element.getKey() );
params.append( "=" );
params.append( element.getValue() );
params.append( "&" );
}
if (params.length() > 0) {
params.deleteCharAt( params.length() - 1 );
}
URL realUrl = new URL( requestUrl );
// 打开和URL之间的连接
httpURLConnection = (HttpURLConnection) realUrl.openConnection();
// 设置通用的请求属性
httpURLConnection.setRequestProperty( "accept", "*/*" );
httpURLConnection.setRequestProperty( "connection", "Keep-Alive" );
httpURLConnection.setRequestProperty( "Content-Length", String.valueOf( params.length() ) );
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput( true );
httpURLConnection.setDoInput( true );
// 获取URLConnection对象对应的输出流
printWriter = new PrintWriter( httpURLConnection.getOutputStream() );
// 发送请求参数
printWriter.write( params.toString() );
// flush输出流的缓冲
printWriter.flush();
// 根据ResponseCode判断连接是否成功
int responseCode = httpURLConnection.getResponseCode();
rspMessage.setStatusCode( responseCode );
// 定义BufferedReader输入流来读取URL的ResponseData
bufferedReader = new BufferedReader( new InputStreamReader( httpURLConnection.getInputStream() ) );
String line;
while ((line = bufferedReader.readLine()) != null) {
responseResult.append( line );
}
rspMessage.setContent( responseResult.toString() );
} catch (Exception e) {
log.error( "send post request error!" + e );
} finally {
httpURLConnection.disconnect();
try {
if (printWriter != null) {
printWriter.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return rspMessage;
}
}