CDN加速

文档中心 API文档 其他 根据客户名称确认客户是否存在

根据客户名称确认客户是否存在

更新时间:2020-06-03 15:36:00

适用产品:其他

接口描述

根据客户名称确认客户是否存在

请求参数

JSON
参数名称类型必填描述
customerNameString完整的客户名称

返回参数

JSON
参数名称类型描述
codeIntegercode
errorMsgStringerrorMsg
isExistingIntegerisExisting

错误码

错误代码(code)描述(message)HTTP状态码语义
2程序失败200数据等问题造成的程序异常

示例

JAVA
示例说明
请求示例
复制
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SimpleTimeZone;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.util.EntityUtils;

public class OpenApiDemo1 {
    /**
     * GET
     */
    public static void get(String url, String username, String apiKey, Map<String, String> header) throws URISyntaxException {
        HttpGet get = new HttpGet();
        get.setURI(new URI(url));
        call(get, username, apiKey, header);
    }

    /**
     * POST
     */
    public static void post(String url, String username, String apiKey, Map<String, String> header, byte[] body) throws URISyntaxException {
        HttpPost post = new HttpPost();
        post.setURI(new URI(url));
        if (body != null) {
            ByteArrayEntity se = new ByteArrayEntity(body);
            post.setEntity(se);
        }
        call(post, username, apiKey, header);
    }

    /**
     * PUT
     */
    public static void put(String url, String username, String apiKey, Map<String, String> header) throws URISyntaxException {
        HttpPut get = new HttpPut();
        get.setURI(new URI(url));
        call(get, username, apiKey, header);
    }

    /**
     * DELETE
     */
    public static void delete(String url, String username, String apiKey, Map<String, String> header) throws URISyntaxException {
        HttpDelete get = new HttpDelete();
        get.setURI(new URI(url));
        call(get, username, apiKey, header);
    }

    /**
     * http call
     */
    private static void call(HttpRequestBase method, String username, String apikey, Map<String, String> header) {
        DefaultHttpClient client = null;
        try {
            client = new DefaultHttpClient();
            addHeader(method, username, apikey, header);
            HttpResponse response = client.execute(method);
            if (response.getStatusLine().getStatusCode() != 200) {
                method.abort();
            }
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String str = EntityUtils.toString(entity);
                System.out.println(str);
            }
        } catch (Exception e) {
            method.abort();
            e.printStackTrace();
        }
    }


    private static void addHeader(AbstractHttpMessage client, String username, String apikey, Map<String, String> headers) throws Exception {
        if (headers != null) {
            for (Iterator<Entry<String, String>> it = headers.entrySet().iterator(); it.hasNext(); ) {
                Entry<String, String> entry = it.next();
                client.addHeader(entry.getKey(), entry.getValue());
            }
        }
        Date date = new Date();
        String dateString = getDate(date);
        String authoriztion = encode(dateString, username, apikey);
        client.addHeader("Date", dateString);
        client.addHeader("Authorization", "Basic " + authoriztion);
    }


    private static String getDate(Date date) {
        SimpleDateFormat rfc822DateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
        rfc822DateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
        String dateString = rfc822DateFormat.format(date);
        return dateString;
    }


    private static String encode(String dateString, String username, String apikey) throws Exception {
        String signature = signAndBase64Encode(dateString.getBytes(), apikey);
        String userAndPwd = username + ":" + signature;
        return new String(Base64.encodeBase64(userAndPwd.getBytes("UTF-8")));
    }


    private static String signAndBase64Encode(byte[] data, String apikey) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA256");//如果使用sha256则参数是:HmacSHA256
        mac.init(new SecretKeySpec(apikey.getBytes(), "HmacSHA256"));//如果使用sha256则参数是:HmacSHA256
        byte[] signature = mac.doFinal(data);
        return new String(Base64.encodeBase64(signature));
    }

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

    	String httpUri = "/api/wserp/check-exist-by-customer-name";
        String httpHost = "https://open.chinanetcenter.com"; //线上环境
        //header
        Map<String, String> header = new HashMap<String, String>();
        header.put("Accept", "application/json");
        header.put("Content-Type", "application/json");
        //username
        String userName = "";
        //apikey
        String apiKey = "";
        //get param
        Map<String,String> param = new HashMap<String,String>();  //构造get 参数
        param.put("customerName","");
        String paramStr = "";
        if( param!=null  && param.size()>0 ){
            for ( String key :param.keySet()) {
                if(!"".equals(paramStr)){
                    paramStr += "&";
                }
                paramStr +=  key + "=" + URLEncoder.encode( param.get( key ),"UTF-8");
            }
        }
        
        String fullHttpUrl = httpHost + httpUri+"?" + paramStr;

        //get示例
        OpenApiDemo1.get(fullHttpUrl, userName, apiKey, null );
    }
}
返回示例
复制
//process success
{"code":"1","errorMsg":"","isExisting":"1"}
{"code":"1","errorMsg":"","isExisting":"2"}
//process failed
{"code":"2","errorMsg":"the error msg"}