http basic authentication通过post方式访问api示例分享 basic认证示例
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);
/**
* 添加创意
*
* @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 = "新增创意:" + creativeaudit.getgeoid() + "出错,状态码:" + statuscode + "; " + createresult;
result.put("failed", errormessage);
}
} catch (exception ue) {
ue.printstacktrace();
result.put("failed", "添加创意时提交的数据有问题!");
}
/*
creativeaudit.setuuid("189-"+creativeaudit.getgeoid());
result.put("success",creativeaudit);
*/
return result;
}