博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Apache HttpComponents 运用
阅读量:3694 次
发布时间:2019-05-21

本文共 1241 字,大约阅读时间需要 4 分钟。

 JSON:

public String sendRequest(String request, String url, String token) throws IOException {        HttpClient httpclient = HttpClients.createDefault();        HttpPost httpPost = new HttpPost(url);        StringEntity entity = new StringEntity(request, "UTF-8");        entity.setContentEncoding("UTF-8");        entity.setContentType("application/json");        httpPost.setEntity(entity);        httpPost.setHeader("authorization", "Bearer " + token);        HttpResponse httpResponse = httpclient.execute(httpPost);        HttpEntity httpEntity = httpResponse.getEntity();        InputStream is = httpEntity.getContent();        ByteArrayOutputStream baos = new ByteArrayOutputStream();        byte[] buffer = new byte[2048];        int bytesRead;        try {            while ((bytesRead = is.read(buffer, 0, 1024)) != -1) {                baos.write(buffer, 0, bytesRead);            }        } catch (IOException ex) {            throw new RuntimeException(ex);        }        byte[] rep = baos.toByteArray();        String reponse = new String(rep, "UTF-8");        return reponse;    }

文件:

......httpPost.setEntity(MultipartEntityBuilder.create().addBinaryBody("file", file, ContentType.DEFAULT_BINARY, fileName).build());......

 

转载地址:http://mwucn.baihongyu.com/

你可能感兴趣的文章
Linux编程考前测试题
查看>>
Openstack面试题和知识点总结
查看>>
C++ 实例化一个对象
查看>>
基于Spring boot+Vue的在线考试系统
查看>>
大数据学习路线
查看>>
前端学习路线
查看>>
推荐几个单机游戏下载网、高质量图片下载网
查看>>
数据库查询
查看>>
单臂路由配置
查看>>
静态路由及动态路由 RIP配置
查看>>
现代密码学:AES
查看>>
现代密码学:密码协议
查看>>
现代密码学:密钥管理
查看>>
数据库增删改
查看>>
RSA公钥
查看>>
【总】现代密码学复习要点总结(谷利泽)
查看>>
【sql-server 数据库 命令大全】
查看>>
数据结构与算法
查看>>
C/C++总结
查看>>
计算机组成原理总结
查看>>