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

相关文章
相关标签/搜索