HTTP Basic authentication access to the API sample through Post method share Basic authentication Example _java

Source: Internet
Author: User
Tags uuid


Copy Code code as follows:

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);

   /**
     * Add Creative
     *
      * @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 = "Add originality:" + creativeaudit.getgeoid () + "error, Status code:" + StatusCode + ";" + Createresult;
Result.put ("Failed", errormessage);
}
\ catch (Exception UE) {
Ue.printstacktrace ();
Result.put ("Failed", "the data submitted when adding the idea has a problem!");
}
/*
Creativeaudit.setuuid ("189-" +creativeaudit.getgeoid ());
Result.put ("Success", Creativeaudit);
*/
return result;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.