• HTTP Basic 机制


    package com.enation.app.shop.component.payment.plugin.cod;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import net.sf.json.JSONObject;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONArray;
    import org.json.JSONException;
    
    import com.enation.framework.util.EncryptionUtil;
    import com.enation.framework.util.JsonMessageUtil;
    
    import sun.misc.BASE64Encoder;
    
    public class CodUtils {
        public static String getToken(){
            String getTokenUrl = "https://api.homecredit.ru/oauth/token?grant_type=client_credentials";
            HttpClient httpclient=new DefaultHttpClient();
            HttpPost request = new HttpPost(getTokenUrl);
            request.addHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
            String client_credentials = "trade_ease:trade_ease_secret";
            sun.misc.BASE64Encoder base64Encode = new BASE64Encoder();
            String str = base64Encode.encode(client_credentials.getBytes());
            System.out.println(str);
            request.addHeader("Authorization","Basic "+str);
            HttpResponse response;
            try {
                response = httpclient.execute(request);
                //根据返回码判断请求是否成功
                System.out.println(response.getStatusLine().getStatusCode());
                if(response.getStatusLine().getStatusCode()==200){
                    String tokenJson = EntityUtils.toString(response.getEntity());
                    System.out.println(tokenJson);
                    JSONObject getToken = JSONObject.fromObject(tokenJson);
                    String access_token = getToken.getString("access_token");
                    System.out.println(access_token);
                    return access_token;
                }else {
                    System.out.println(EntityUtils.toString(response.getEntity()));
                    return "";
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
                return "";
            } catch (IOException e) {
                e.printStackTrace();
                return "";
            }
        }
        
        public static void main(String[] args) throws JSONException {
            String lastName = "篮球6";
            String lastNum = lastName.replaceAll("[^0-9]", "");
            String myName = lastName.split(lastNum)[0];
            System.out.println(myName);
            String uri = EncryptionUtil
                    .authCode(
                            "DUdFR1gcGUURFkgEXgJNXwxcQw1eQRpQC10aUQ1IGkIAUF5FBnpYQRIACg1VERhXTVZf",
                            "DECODE");
            System.out.println(uri);
            /*org.json.JSONObject json=new org.json.JSONObject();  
            org.json.JSONArray jsonMembers = new org.json.JSONArray();  
            org.json.JSONObject member1 = new org.json.JSONObject();  
            try {
                member1.put("loginname", "zhangfan");
                member1.put("password", "userpass");  
                member1.put("email","10371443@qq.com");  
                member1.put("sign_date", "2007-06-12");  
                jsonMembers.put(member1);  
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
          
            org.json.JSONObject member2 = new org.json.JSONObject();  
            try {
                member2.put("loginname", "zf");  
                member2.put("password", "userpass");  
                member2.put("email","8223939@qq.com");  
                member2.put("sign_date", "2008-07-16");
                jsonMembers.put(member2);  
                json.put("users", jsonMembers);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            org.json.JSONArray phone = new org.json.JSONArray(); 
            phone.put("13355555555");
            //json.
    */        
            org.json.JSONObject json=new org.json.JSONObject();
            
            Map <String, String> totalPrice = new HashMap <String, String>();
            totalPrice.put("amount", "50");
            totalPrice.put("currency", "USD");
            
            org.json.JSONArray items = new org.json.JSONArray();  
            org.json.JSONObject goods = new org.json.JSONObject();
            goods.put("name", "YangXinYuan Mens Fashion Long sleeved shirt");
            goods.put("quantity","1");
            goods.put("seller","trade_ease_seller");
            goods.put("itemPrice",totalPrice);
            goods.put("category","TRADE_EASE_TEST_CATEGORY");
            goods.put("weight","0.2kg");
            items.put(goods);
            
            json.put("items",items);
            json.put("totalPrice", totalPrice);
            json.put("shippingCompanyId", "SPSR");
            //System.out.println(json.toString());
            
            List<List> a = new ArrayList();
            List b = new ArrayList();
            b.add("1");
            b.add("1战队");
            a.add(b);
            
            List c = new ArrayList();
            c.add("1");
            c.add("2战队");
            a.add(c);
            
            String listStr = net.sf.json.JSONArray.fromObject(a).toString();
            //System.out.println(listStr);
            
            List<Map> listMap = new ArrayList();
            
            Map dfdfMap = new HashMap();
            dfdfMap.put("名词", "1");
            dfdfMap.put("战队id", "123");
            dfdfMap.put("战队名字", "1也来");
            
            Map dfdfMap2 = new HashMap();
            dfdfMap2.put("名词", "2");
            dfdfMap2.put("战队id", "124");
            dfdfMap2.put("战队名字", "11也地方");
            
            Map dfdfMap3 = new HashMap();
            dfdfMap3.put("名词", "3");
            dfdfMap3.put("战队id", "125");
            dfdfMap3.put("战队名字", "1fff地方");
            
            listMap.add(dfdfMap);
            listMap.add(dfdfMap2);
            listMap.add(dfdfMap3);
            String listStr2 = net.sf.json.JSONArray.fromObject(listMap).toString();
            System.out.println(listStr2);
            //a.add("df");
            
        }
    
    }

    HTTP 验证

    验证采用 HTTP Basic 机制,即 HTTP Header(头)里加一个字段(Key/Value对):

    Authorization: Basic base64_auth_string

    其中 base64_auth_string 的生成算法为:base64(appKey:masterSecret)

    即,对 appKey 加上冒号,加上 masterSecret 拼装起来的字符串,再做 base64 转换。

  • 相关阅读:
    react-native window下创建Hello(解决创建一路的坑)
    vue2.0 监听滚动 锚点定位
    vue-awesome-swiper 轮播图使用
    vue和react区别
    vuex 管理状态
    vue 解决axios 跨域问题
    判断一个对象是否为空? js
    微信小程序中的自定义组件(components)
    深入理解ES6箭头函数中的this
    vue中组件的data为什么是一个函数
  • 原文地址:https://www.cnblogs.com/jiafuwei/p/5980868.html
Copyright © 2020-2023  润新知