HttpClient Post 二进制/字节流/byte[]

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

 
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
httpClient是一种开源的HTTP通信库,它可以用来发送HTTP请求和接收HTTP响应。在进行网络通信时,传输的数据有两种形式,即文本和字节流。 文本是由可读的字符组成的数据,如普通的文本文件。在HTTP通信,文本数据通常是以UTF-8编码的字符串形式进行传输的。文本数据的优点是可读性好,易于理解和处理。然而,由于文本是由字符组成的,而不是字节,所以在网络传输时需要进行编码和解码操作才能正确地传输和接收文本数据。在httpClient,可以使用StringEntity类将文本数据转换为字节流进行传输。 字节流是由二进制字节组成的数据,没有直接的可读性。在HTTP通信字节流通常用于传输二进制类型的数据,如图片、视频、音频等。字节流的优点是可以精确地表示和传输任意类型的数据,无需进行编码和解码操作,效率较高。在httpClient,可以使用ByteArrayEntity类将字节流数据进行传输。 无论是文本还是字节流,在使用httpClient发送HTTP请求时,都需要将数据按照特定的格式转换为请求体,并在请求头设置Content-Type字段来告知服务器所发送的数据的类型。同样地,在接收HTTP响应时,httpClient会根据响应的Content-Type字段来判断接收到的数据类型,然后将其转换为对应的文本或字节流。 综上所述,httpClient可以通过将文本数据转换为字节流进行网络传输,以满足不同类型数据的发送和接收需求。无论是传输文本数据还是字节流数据,httpClient都提供了相应的类和方法来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值