E-commerce Electronic Surface single setup-Express Bird API interface

Source: Internet
Author: User
Tags finally block http post md5 encryption

(1) Electronic Surface single interface is a courier bird to provide independent e-commerce, warehousing management systems, logistics and supply chain logistics system platform for the use of the next single interface.

(2) for customers to solve the online delivery needs, merchants through the network Select Courier Company sent a request to inform The courier company has express to be shipped.

(3) The customer forwarded the data through this interface to The Courier bird, by the Courier bird for you to arrange the Courier home Pickup service.

(4) The order number (Ordercode) cannot be submitted repeatedly, the system will return the specific error code.

(5) The message received by the interface is HTTP POST, the encoding format of the request method (Utf-8):"application/ X-www-form-urlencoded;charset=utf-8 ".

( 6

Import java.io.bufferedreader;import java.io.ioexception; import java.io.inputstreamreader ;import java.io.outputstreamwriter;import java.io.unsupportedencodingexception;import  java.net.httpurlconnection;import java.net.url;import java.net.urlencoder;import  Java.util.hashmap;import java.util.map;import com.sun.org.apache.xerces.internal.impl.dv.util.base64 ;import java.security.messagedigest; /** * *  Express Bird Electronic Surface single interface  * */ public  class kdgoldapidemo {//e-commerce idprivate string ebusinessid= "please go to the Express Bird website/http// Www.kdniao.com/ServiceApply.aspx ";//e-commerce encryption private key, courier Bird provide, pay attention to safekeeping, do not leak private string appkey=" Please go to the Express Bird website/http Www.kdniao.com/ServiceApply.aspx ";//Request url,  formal environment address:http://api.kdniao.cc/api/eorderserviceprivate  String requrl= "Http://testapi.kdniao.cc:8081/api/Eorderservice";/**     *  JSON mode   e-form  *  @throws  exception &Nbsp;    */public string orderonlinebyjson ()  throws exception{string  requestData=  "{' Ordercode ':  ' 012657700387 ',"  +                  "' Shippercode ': ' EMS ',"  +                  "' PayType ': 1,"  +                  "' Exptype ': 1,"  +                  ' cost ': 1.0, '  +                 "' Othercost ': 1.0, " +                  ' Sender ': '  +                  "{"  +                 "' Company ': ' LV ', ' Name ': ' Taylor ', ' Mobile ': ' 15018442396 ', ' provincename ': ' Shanghai ', ' cityname ': ' Shanghai ', ' expareaname ': ' Qingpu ', ' Address ': ' Pearl Road No. 73rd '}, '  +                 "' Receiver ': '  +                  "{"  +                  "' Company ': ' Gccui ', ' Name ': ' Yann ', ' Mobile ': ' 15018442396 ', ' provincename ': ' Beijing ', ' cityname ': ' Beijing ', ' expareaname ' ': ' Chaoyang District ', ' Address ': ' Three Sanlitun street yashow building ', '  +                  "' Commodity ':"  +                  "[{"  +                  "' goodsname ': ' Shoes ', ' goodsquantity ': 1, ' Goodsweight ': 1.0}],"  +                  "' Weight ': 1.0,"  +                  "' Quantity ': 1,"  +                  "' Volume ': 0.0,"  +                  "' Remark ': ' Handle with care ', '  +                 "' Isreturnprinttemplate ': 1}"; Map<string, string> params = new hashmap<string, string> (); Params.put ("RequestData",  urlencoder (requestdata,  "UTF-8"));p arams.put ("Ebusinessid",  EBUSINESSID);p arams.put ("RequestType",  "1007"); String datasign=encrypt (requestdata, appkey,  "UTF-8");p Arams. put ("Datasign",  urlencoder (datasign,  "UTF-8"));p arams.put ("DataType",  "2"); String result=sendpost (requrl, params);//The information returned according to the company's business process ... return result;} /**&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;*&NBSP;MD5 encryption      *  @param  str  content             *  @param  charset  Encoding method  *  @throws  exception      */@SuppressWarnings ("unused") private &NBSP;STRING&NBSP;MD5 (String str, string charset)  throws Exception {     messagedigest md = messagedigest.getinstance ("MD5");     Md.update (Str.getbytes (CharSet));     byte[] result = md.digest ();     stringbuffer sb = new stringbuffer (+);    for  (Int i = 0; i < result.length; i+ +)  {        int val = result[i] &  0xff;        if  (VAL&NBSP;&LT;=&NBSP;0XF)  {             sb.append ("0");         }        sb.append (Integer.toHexString (val));     }    return sb.tostring (). toLowerCase ();} /**&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;*&NBSP;BASE64 encoding      *  @param  str  content             *  @param  charset  Encoding method  *  @throws  UnsupportedEncodingException      */private  String base64 (String str, string charset)  throws unsupportedencodingexception{ String encoded = base64.encode (Str.getbytes (CharSet) return encoded;    } @SuppressWarnings ("unused") private string  Urlencoder (String str, string charset)  throws unsupportedencodingexception{string  result = urlencoder.encode (str, charset); return result;} /**     *  sign Generation      *  @param  content   Content         *  @param  keyValue Appkey        *  @param  charset  encoding  *  @throws   unsupportedencodingexception ,exception *  @return  datasign Signature       */@SuppressWarnings ("unused") private string encrypt  (string content, string  Keyvalue, string charset)  throws UnsupportedEncodingException, Exception{if  ( Keyvalue != null) {return base64 (MD5 (content + keyvalUe, charset),  charset);} Return base64 (MD5 (Content, charset),  charset);}  /**     *  request to specify  URL  send a POST method            *  @param  url  Send Request  URL          *  @param  params  parameter collection for requests            *  @return   Remote resource response results      */@SuppressWarnings ("unused") private  String sendpost (String url, map<string, string> params)  {         OutputStreamWriter out = null;         BufferedReader in = null;                 StringBuilder result = new  StringBuilder ();          try {            url  Realurl = new url (URL);             httpurlconnection conn = (HttpURLConnection)  realurl.openconnection ();             //  send a POST request must set the following two lines              conn.setdooutput (True);             conn.setdoinput (True);             // post Methods              Conn.setrequestmethod ("POST");            //  Set the common Request property             conn.setrequestproperty (" Accept ", " */* ");    &nbsP;        conn.setrequestproperty ("Connection",  "Keep-Alive");             conn.setrequestproperty ("User-agent",                       "mozilla/4.0  (compatible; msie 6.0; windows nt 5.1; SV1);             conn.setrequestproperty (" Content-type ", " application/x-www-form-urlencoded ");             conn.connect ();             //  get the output stream corresponding to the URLConnection object              Out = new outputstreamwriter (Conn.getoutputstream (),  "UTF-8");             //  Send Request parameters                          if  (params != null)  {           stringbuilder param = new stringbuilder ();            for  (map.entry<string, string>  Entry : params.entryset ())  {          if ( Param.length () >0) {          param.append ("&");           }                     param.append (Entry.getKey ());           param.append ("=");           param.append (Entry.getvalue ());                     system.out.println (Entry.getkey () + ":" +entry.getvalue ());           }           System.out.println ("param:" +param.tostring ());           Out.write (Param.tostring ());            }  Buffering of            // flush output streams              out.flush ();             //  define BufferedReader input stream to read the response of the URL              in = new bufferedreader (              &nbSp;      new inputstreamreader (Conn.getinputstream (),  "UTF-8"));             String line;             while  ((Line = in.readline ())  != null )  {                 Result.append (line);            }         } catch  (exception e)  {                          e.printstacktrace ();        }         //using the finally block to close the output stream, input stream         finally{             try{                 if (out!=null) {                     out.close ();                 }                 if (in!=null) {                     in.close ();                 }             }            catch (IOException &NBSP;EX) {                 Ex.printstacktrace ();             }         }        return result.tostring ();     } }



E-commerce Electronic Surface single setup-Express Bird API interface

Related Article

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.