博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClient Post 二进制/字节流/byte[]
阅读量:7293 次
发布时间:2019-06-30

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

HttpClient 3.x

public class HttpHelper {    String m_url;    HttpClient m_HttpClient;    public HttpHelper(String url) {        m_url = url;        m_HttpClient = new HttpClient();    }    public byte[] post(byte[] bytes, String contentType) throws IOException {        PostMethod method = new PostMethod(m_url);        if ((contentType != null) && (contentType.length() > 0))            method.setRequestHeader("Content-type" , contentType);        method.setRequestEntity(new ByteArrayRequestEntity(bytes));        int HttpCode = m_HttpClient.executeMethod(method);        if (HttpCode != HttpStatus.SC_OK)            throw new IOException("Invalid HttpStatus: " + HttpCode);        InputStream respStream = method.getResponseBodyAsStream();        int respBodySize = respStream.available();        if (respBodySize <= 0)            throw new IOException("Invalid respBodySize: " + respBodySize);        byte[] respBuffer = new byte[respBodySize];        if (respStream.read(respBuffer) != respBodySize)            throw new IOException("Read respBody Error");        return respBuffer;    }    public String postXml(String str) throws IOException {        byte[] reqBuffer = str.getBytes(Charset.forName("UTF-8"));        byte[] respBuffer = post(reqBuffer, "application/xml; charset=UTF-8");        String resp = new String(respBuffer, Charset.forName("UTF-8"));        return resp;    }}

HttpClient 4.x

public class HttpHelper {    CloseableHttpClient m_HttpClient;    public HttpHelper() {        m_HttpClient = HttpClients.createDefault();    }    // send bytes and recv bytes    public byte[] post(String url, byte[] bytes, String contentType) throws IOException {        HttpPost httpPost = new HttpPost(url);        httpPost.setEntity(new ByteArrayEntity(bytes));        if (contentType != null)            httpPost.setHeader("Content-type", contentType);        CloseableHttpResponse httpResponse = m_HttpClient.execute(httpPost);        try {            HttpEntity entityResponse = httpResponse.getEntity();            int contentLength = (int) entityResponse.getContentLength();            if (contentLength <= 0)                throw new IOException("No response");            byte[] respBuffer = new byte[contentLength];            if (entityResponse.getContent().read(respBuffer) != respBuffer.length)                throw new IOException("Read response buffer error");            return respBuffer;        } finally {            httpResponse.close();        }    }    public byte[] post(String url, byte[] bytes) throws IOException {        return post(url, bytes, null);    }    public String postXml(String url, String str) throws IOException {        byte[] reqBuffer = str.getBytes(Charset.forName("UTF-8"));        byte[] respBuffer = post(url, reqBuffer, "application/xml; charset=UTF-8");        String resp = new String(respBuffer, Charset.forName("UTF-8"));        return resp;    }}

 

转载于:https://www.cnblogs.com/java-jun-world2099/articles/9069113.html

你可能感兴趣的文章
WCF学习
查看>>
django 基础进 COOKIE
查看>>
[Java 8] (10) 使用Lambda完成函数组合,Map-Reduce以及并行化
查看>>
@EnableWebMvc
查看>>
eclipse中输入的中文为繁体的问题
查看>>
.NET跨平台:在Linux Ubuntu上编译coreclr/corefx/dnx(20150617)
查看>>
[CQOI2016]手机号码
查看>>
Eclipse CDT 配置C /C ++ 标准库 (UBUNTU 12 )
查看>>
面霸吕国栋之:整理的一些面试题
查看>>
转 Python爬虫入门五之URLError异常处理
查看>>
转 Python执行系统命令的方法
查看>>
CSS 折角效果
查看>>
个人作业3---个人总结
查看>>
[分享]ip地址爬取过滤的shell
查看>>
差分数组
查看>>
Shiro 加密helloWorld
查看>>
关于安装sql2012出现的netfx3功能问题
查看>>
基础关3
查看>>
tar 解压缩
查看>>
(转)Sharepoint学习笔记—Debug--寻找 WSS_Logging下的ULSTraceLog
查看>>