java代碼調用第三方接口

1、利用httpclient來字符串參數(url是第三方接口,不帶參數,如:http://192.168.16.200:8081/faceInfo/list,param是url後面所要帶的參數)json

public static JSONObject getHttpResponseJson(String url,Map<String,String> param){
        CloseableHttpClient httpclient = null;
        CloseableHttpResponse response = null;
        JSONObject jsonObject = null;
        
        try {
            //請求發起客戶端
            httpclient = HttpClients.createDefault();
            //參數集合
            List postParams = new ArrayList();
            //遍歷參數並添加到集合
            for(Map.Entry<String, String> entry:param.entrySet()){
                postParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
            //經過post方式訪問
            HttpPost post = new HttpPost(url);
            HttpEntity paramEntity = new UrlEncodedFormEntity(postParams,"UTF-8");
            post.setEntity(paramEntity);
            response = httpclient.execute(post);
            HttpEntity valueEntity = response.getEntity();
            String content = EntityUtils.toString(valueEntity);
            jsonObject = JSONObject.fromObject(content);
            
            return jsonObject;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(httpclient != null){
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return jsonObject;
    }

2、利用httpclient來同時上傳文件和其餘字符串參數(postUrl請求地址,第三方接口,不帶參數,如:http://192.168.16.200:8081/faceInfo/list,filePathParam封裝文件的上傳路徑,param封裝參數)post

public static String getHttpResponseString(String postUrl,Map<String,String> filePathParam,Map<String,String> param){
        //1:建立一個httpclient對象
        HttpClient httpclient = new DefaultHttpClient();
        Charset charset = Charset.forName("UTF-8");//設置編碼
        try {
            //2:建立http的發送方式對象,是GET仍是post
            HttpPost httppost = new HttpPost(postUrl);

            //3:建立要發送的實體,就是key-value的這種結構,藉助於這個類,能夠實現文件和參數同時上傳
            MultipartEntity reqEntity = new MultipartEntity();
            
            if (filePathParam != null) {
                //遍歷圖片並添加到MultipartEntity實體中
                for(Map.Entry<String, String> entry:filePathParam.entrySet()){
                     FileBody fileContent = new FileBody(new File(entry.getValue()));
                     reqEntity.addPart(entry.getKey(),fileContent);
                }
            }
            
            if (param != null) {
                //遍歷參數並添加到MultipartEntity實體中
                for(Map.Entry<String, String> entry:param.entrySet()){
                    StringBody content = new StringBody(entry.getValue(),charset);
                    reqEntity.addPart(entry.getKey(), content);
                }
            }
            
            httppost.setEntity(reqEntity);

            //4:執行httppost對象,從而得到信息
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();

            //得到返回來的信息,轉化爲字符串string
            String resString = EntityUtils.toString(resEntity);
            return resString;

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
        }
        return null;
    }
相關文章
相關標籤/搜索