java代碼調用第三方介面

來源:互聯網
上載者:User

標籤:add   通過   tco   檔案   content   發送   div   map   方式   

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

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;    }

二、利用httpclient來同時上傳檔案和其他字串參數(postUrl)

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();                        //遍曆圖片並添加到MultipartEntity實體中            for(Map.Entry<String, String> entry:filePathParam.entrySet()){                 FileBody fileContent = new FileBody(new File(entry.getValue()));                 reqEntity.addPart(entry.getKey(),fileContent);            }                        //遍曆參數並添加到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;    }

 

java代碼調用第三方介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.