http basic authentication通過post方式訪問api示例分享 basic認(rèn)證示例
private static String url = PropertiesLoader.getProperty("ALLYES_SERVER", false);
private static String username = PropertiesLoader.getProperty("ALLYES_USERNAME", false);
private static String password = PropertiesLoader.getProperty("ALLYES_PASSWORD", false);
/**
* 添加創(chuàng)意
*
* @param creativeAudit
* @return
*/
public static Map<String, Object> addCreative(CreativeAudit creativeAudit) {
//name,width,height,type,creativeTagId, code,bindId
String type = "9";
if (creativeAudit.getRelative_path().toLowerCase().endsWith("gif"))
type = "10";
if (creativeAudit.getRelative_path().toLowerCase().endsWith("swf"))
type = "11";
Map<String, Object> result = new HashMap<String, Object>();
String addUrl = url + "/creatives/add";
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
try {
List<NameValuePair> postparams = new ArrayList<NameValuePair>();
postparams.add(new BasicNameValuePair("name", creativeAudit.getName()));
postparams.add(new BasicNameValuePair("width", Integer.toString(creativeAudit.getWidth())));
postparams.add(new BasicNameValuePair("height", Integer.toString(creativeAudit.getHeight())));
postparams.add(new BasicNameValuePair("type", type));
postparams.add(new BasicNameValuePair("creativeTagId", creativeAudit.getAdCategory().getAd_caterory().substring(2)));
postparams.add(new BasicNameValuePair("code", creativeAudit.getCode()));
postparams.add(new BasicNameValuePair("bindId", creativeAudit.getGeoid()));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postparams, "UTF-8");
HttpPost httpPost = new HttpPost(addUrl);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity httpEntity = httpResponse.getEntity();
String createResult = EntityUtils.toString(httpEntity, "UTF-8");
JSONObject jsonObject = JSONObject.fromObject(createResult);
String uuid = jsonObject.get("id").toString();
creativeAudit.setUuid(uuid);
result.put("success", creativeAudit);
} else {
HttpEntity httpEntity = httpResponse.getEntity();
String createResult = EntityUtils.toString(httpEntity, "UTF-8");
String errorMessage = "新增創(chuàng)意:" + creativeAudit.getGeoid() + "出錯,狀態(tài)碼:" + statusCode + "; " + createResult;
result.put("failed", errorMessage);
}
} catch (Exception ue) {
ue.printStackTrace();
result.put("failed", "添加創(chuàng)意時提交的數(shù)據(jù)有問題!");
}
/*
creativeAudit.setUuid("189-"+creativeAudit.getGeoid());
result.put("success",creativeAudit);
*/
return result;
}
相關(guān)文章
java構(gòu)造函數(shù)的三種類型總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于java構(gòu)造函數(shù)的三種類型總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2021-01-01
一文總結(jié)RabbitMQ中的消息確認(rèn)機制
RabbitMQ消息確認(rèn)機制指的是在消息傳遞過程中,發(fā)送方發(fā)送消息后,接收方需要對消息進(jìn)行確認(rèn),以確保消息被正確地接收和處理,本文為大家整理了RabbitMQ中的消息確認(rèn)機制,需要的可以參考一下2023-06-06
SpringBoot?+?MyBatis-Plus構(gòu)建樹形結(jié)構(gòu)的幾種方式
在實際開發(fā)中,很多數(shù)據(jù)都是樹形結(jié)構(gòu),本文主要介紹了SpringBoot?+?MyBatis-Plus構(gòu)建樹形結(jié)構(gòu)的幾種方式,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
springboot下添加全局異常處理和自定義異常處理的過程解析
在spring項目中,優(yōu)雅處理異常,好處是可以將系統(tǒng)產(chǎn)生的全部異常統(tǒng)一捕獲處理,自定義的異常也由全局異常來捕獲,如果涉及到validator參數(shù)校驗器使用全局異常捕獲也是較為方便,這篇文章主要介紹了springboot下添加全局異常處理和自定義異常處理,需要的朋友可以參考下2023-12-12
springboot高并發(fā)下提高吞吐量的實現(xiàn)
這篇文章主要介紹了springboot高并發(fā)下提高吞吐量的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

