HttpClient發送GET和POST請求

GET請求:java

   @Override
    public Object Get(List<SysNews> sysNewsList,Integer num, Integer offset) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        
        try {
            // 簽名
            String utf8Tag = java.net.URLEncoder.encode(tag,"utf-8");
            String uri = String.format(GET_FEEDS_URI, utf8Tag, num, offset);
            String auth_string = ak + "\n" + auth_time + "\n" + rand_num + "\n" + uri;
            String encrypt_string = HUAJIAOSign.Base64.encode(HUAJIAOSign.hmacSha1(auth_string, sk));
            String authorization = ak + ":" + encrypt_string;//
            
            String url = String.format(GET_FEEDS_URL, utf8Tag, num, offset);
            HttpGet httpget = new HttpGet(url);  
            
            //header中傳遞的參數
            httpget.setHeader("Authorization", authorization);//簽名
            httpget.setHeader("Channelid", Channelid);//開放平臺建立應用是系統分配的channelID
            httpget.setHeader("Auth-Time", auth_time);//請求時間戳
            httpget.setHeader("Rand-Num", rand_num);//隨機數
            
            //執行請求
            HttpResponse httpResponse = httpClient.execute(httpget);
            if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                    String result = EntityUtils.toString(httpResponse.getEntity());// 返回json格式:
                    response = JSONObject.fromObject(result);
                }
            } catch (Exception e) {
                log.error(e.toString());
            }
        
        return response;  
    }

 

POST請求:json

    public static Object doPay(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        
        JSONObject response = null;
        try {
            StringBuilder sb = new StringBuilder();
            // 簽名
            String auth_string = ak + "\n" + auth_time + "\n" + rand_num + "\n" + GET_FEEDS_URI;
            String encrypt_string = HUAJIAOSign.Base64.encode(HUAJIAOSign.hmacSha1(auth_string, sk));
            String authorization = ak + ":" + encrypt_string;//
            
            log.info("  authorization======" + authorization);ide

            HttpPost post = new HttpPost(url);  
            
            //header中傳遞的參數
            post.setHeader("Authorization", authorization);//簽名
            post.setHeader("Channelid", "qianqianluhui");//開放平臺建立應用是系統分配的channelID
            post.setHeader("Auth-Time", auth_time);//請求時間戳
            post.setHeader("Rand-Num", rand_num);//隨機數
            
            //請求體
            //建立參數列表
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            list.add(new BasicNameValuePair("platform", "server"));// 平臺(固定值傳server)
            list.add(new BasicNameValuePair("tag", "video"));//標籤
            list.add(new BasicNameValuePair("num", "1"));// 每次返回數量(默認50條記錄,取值範圍1~100)
            list.add(new BasicNameValuePair("offset", "0"));// 偏移量,上次請求返回offset值,首次傳0或不傳
            //url格式編碼
            UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(list,"UTF-8");
            post.setEntity(uefEntity);

            //執行請求
            HttpResponse httpResponse = httpClient.execute(post);
            if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                HttpEntity entity = httpResponse.getEntity();
                String result = EntityUtils.toString(httpResponse.getEntity());// 返回json格式:
                response = JSONObject.fromObject(result);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        
        return response;
    }post

相關文章
相關標籤/搜索