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() + "出錯(cuò),狀態(tài)碼:" + statusCode + "; " + createResult;
result.put("failed", errorMessage);
}
} catch (Exception ue) {
ue.printStackTrace();
result.put("failed", "添加創(chuàng)意時(shí)提交的數(shù)據(jù)有問題!");
}
/*
creativeAudit.setUuid("189-"+creativeAudit.getGeoid());
result.put("success",creativeAudit);
*/
return result;
}
相關(guān)文章
Spring框架基于xml實(shí)現(xiàn)自動(dòng)裝配流程詳解
自動(dòng)裝配就是指?Spring?容器在不使用?<constructor-arg>?和<property>?標(biāo)簽的情況下,可以自動(dòng)裝配(autowire)相互協(xié)作的?Bean?之間的關(guān)聯(lián)關(guān)系,將一個(gè)?Bean?注入其他?Bean?的?Property?中2022-11-11
java構(gòu)造函數(shù)的三種類型總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于java構(gòu)造函數(shù)的三種類型總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2021-01-01
一文總結(jié)RabbitMQ中的消息確認(rèn)機(jī)制
RabbitMQ消息確認(rèn)機(jī)制指的是在消息傳遞過程中,發(fā)送方發(fā)送消息后,接收方需要對消息進(jìn)行確認(rèn),以確保消息被正確地接收和處理,本文為大家整理了RabbitMQ中的消息確認(rèn)機(jī)制,需要的可以參考一下2023-06-06
JavaWeb評論功能實(shí)現(xiàn)步驟以及代碼實(shí)例
項(xiàng)目初始版本上線,有時(shí)間寫點(diǎn)東西記錄一下項(xiàng)目中的心得體會(huì),通過這個(gè)項(xiàng)目學(xué)習(xí)了很多,要寫下來的有很多,先從評論功能開始吧,下面這篇文章主要給大家介紹了關(guān)于JavaWeb評論功能實(shí)現(xiàn)步驟以及代碼的相關(guān)資料,需要的朋友可以參考下2023-01-01
SpringBoot?+?MyBatis-Plus構(gòu)建樹形結(jié)構(gòu)的幾種方式
在實(shí)際開發(fā)中,很多數(shù)據(jù)都是樹形結(jié)構(gòu),本文主要介紹了SpringBoot?+?MyBatis-Plus構(gòu)建樹形結(jié)構(gòu)的幾種方式,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
springboot下添加全局異常處理和自定義異常處理的過程解析
在spring項(xiàng)目中,優(yōu)雅處理異常,好處是可以將系統(tǒng)產(chǎn)生的全部異常統(tǒng)一捕獲處理,自定義的異常也由全局異常來捕獲,如果涉及到validator參數(shù)校驗(yàn)器使用全局異常捕獲也是較為方便,這篇文章主要介紹了springboot下添加全局異常處理和自定義異常處理,需要的朋友可以參考下2023-12-12
springboot高并發(fā)下提高吞吐量的實(shí)現(xiàn)
這篇文章主要介紹了springboot高并發(fā)下提高吞吐量的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

