|
@@ -24,10 +24,10 @@ import java.util.Map;
|
|
|
public class InterfaceConnUtils {
|
|
|
|
|
|
/**
|
|
|
- * post请求-推数据
|
|
|
+ * post请求-推数据,Token认证:Basic Auth
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static JSONObject doPost(JSONObject json, String url,String type) throws Exception {
|
|
|
+ public static JSONObject doPostToBasicAuth(JSONObject json, String url) throws Exception {
|
|
|
//定义测试返回JSON
|
|
|
JSONObject onejb = new JSONObject();
|
|
|
HttpClient httpClient = new DefaultHttpClient();
|
|
@@ -60,5 +60,47 @@ public class InterfaceConnUtils {
|
|
|
}
|
|
|
return onejb;
|
|
|
}
|
|
|
+ /**
|
|
|
+ * post请求-推数据,Token认证:Bearer Token
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static JSONObject doPostToBearer(JSONObject json, String url) throws Exception {
|
|
|
+ //定义返回JSON
|
|
|
+ JSONObject onejb = new JSONObject();
|
|
|
+ String token=null;
|
|
|
+ HttpClient httpClient = new DefaultHttpClient();
|
|
|
+ //获取token的Url
|
|
|
+ String tokenUrl="https://zoneda.onestep-cloud.com/oauth/oauth/token?grant_type=client_credentials&client_id=SY-NO-PASS-LOGIN&client_secret=SY-NO-PASS-LOGIN";
|
|
|
+ try {
|
|
|
+ HttpPost hpGetToken=new HttpPost(tokenUrl);
|
|
|
+ //调用接口
|
|
|
+ HttpResponse response = httpClient.execute(hpGetToken);
|
|
|
+ //返回信息
|
|
|
+ String resultTokenStr = EntityUtils.toString(response.getEntity());
|
|
|
+ JSONObject tokenjb=JSONObject.parseObject(resultTokenStr);
|
|
|
+ token=tokenjb.getString("access_token");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ HttpPost httpPost=new HttpPost(url);
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
+ httpPost.setHeader("Authorization","Bearer " + token);
|
|
|
|
|
|
+ StringEntity entity;
|
|
|
+ entity = new StringEntity(json.toString(),"utf-8");
|
|
|
+ entity.setContentEncoding("utf-8");
|
|
|
+ entity.setContentType("application/json");
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ try {
|
|
|
+ //调用接口
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+ //返回信息
|
|
|
+ String resultStr = EntityUtils.toString(response.getEntity());
|
|
|
+ onejb=JSONObject.parseObject(resultStr);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return onejb;
|
|
|
+ }
|
|
|
}
|