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)文章
SpringBoot 使用WebSocket功能(實(shí)現(xiàn)步驟)
本文通過詳細(xì)步驟介紹了SpringBoot 使用WebSocket功能,首先需要導(dǎo)入WebSocket坐標(biāo),編寫WebSocket配置類,用于注冊WebSocket的Bean,結(jié)合示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-02-02Java實(shí)現(xiàn)解析JSON大文件JsonReader工具詳解
這篇文章主要介紹了Java實(shí)現(xiàn)解析JSON大文件的工具JsonReader使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01關(guān)于WeakhashMap與HashMap之間的區(qū)別和聯(lián)系
這篇文章主要介紹了關(guān)于WeakhashMap與HashMap之間的區(qū)別和聯(lián)系,WeakHashMap從名字可以得知主要和Map有關(guān),不過還有一個(gè)Weak,我們就更能自然而然的想到這里面還牽扯到一種弱引用結(jié)構(gòu),因此想要徹底搞懂,我們還需要知道四種引用,需要的朋友可以參考下2023-09-09spring @Validated 注解開發(fā)中使用group分組校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了spring @Validated 注解開發(fā)中使用group分組校驗(yàn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05elasticsearch索引index之put?mapping的設(shè)置分析
這篇文章主要為大家介紹了elasticsearch索引index之put?mapping的設(shè)置分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04SpringBoot使用Sa-Token實(shí)現(xiàn)賬號封禁、分類封禁、階梯封禁的示例代碼
本文主要介紹了SpringBoot使用Sa-Token實(shí)現(xiàn)賬號封禁、分類封禁、階梯封禁的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Struts2學(xué)習(xí)筆記(3)-DMI動(dòng)態(tài)調(diào)用方式
本文主要介紹Struts2的DMI動(dòng)態(tài)調(diào)用的兩種方式,簡單實(shí)用,希望能給大家做一個(gè)參考。2016-06-06