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

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

  6790人阅读  评论(0)  收藏  举报
  分类:

目录(?)[+]

HttpClient 3.x

[java]  view plain  copy
  1. public class HttpHelper {  
  2.     String m_url;  
  3.     HttpClient m_HttpClient;  
  4.   
  5.     public HttpHelper(String url) {  
  6.         m_url = url;  
  7.         m_HttpClient = new HttpClient();  
  8.     }  
  9.   
  10.     public byte[] post(byte[] bytes, String contentType) throws IOException {  
  11.         PostMethod method = new PostMethod(m_url);  
  12.   
  13.         if ((contentType != null) && (contentType.length() > 0))  
  14.             method.setRequestHeader("Content-type" , contentType);  
  15.         method.setRequestEntity(new ByteArrayRequestEntity(bytes));  
  16.         int HttpCode = m_HttpClient.executeMethod(method);  
  17.         if (HttpCode != HttpStatus.SC_OK)  
  18.             throw new IOException("Invalid HttpStatus: " + HttpCode);  
  19.         InputStream respStream = method.getResponseBodyAsStream();  
  20.         int respBodySize = respStream.available();  
  21.         if (respBodySize <= 0)  
  22.             throw new IOException("Invalid respBodySize: " + respBodySize);  
  23.         byte[] respBuffer = new byte[respBodySize];  
  24.         if (respStream.read(respBuffer) != respBodySize)  
  25.             throw new IOException("Read respBody Error");  
  26.         return respBuffer;  
  27.     }  
  28.   
  29.     public String postXml(String str) throws IOException {  
  30.         byte[] reqBuffer = str.getBytes(Charset.forName("UTF-8"));  
  31.         byte[] respBuffer = post(reqBuffer, "application/xml; charset=UTF-8");  
  32.         String resp = new String(respBuffer, Charset.forName("UTF-8"));  
  33.         return resp;  
  34.     }  
  35. }  



HttpClient 4.x

[java]  view plain  copy
  1. public class HttpHelper {  
  2.     CloseableHttpClient m_HttpClient;  
  3.   
  4.     public HttpHelper() {  
  5.         m_HttpClient = HttpClients.createDefault();  
  6.     }  
  7.   
  8.     // send bytes and recv bytes  
  9.     public byte[] post(String url, byte[] bytes, String contentType) throws IOException {  
  10.         HttpPost httpPost = new HttpPost(url);  
  11.         httpPost.setEntity(new ByteArrayEntity(bytes));  
  12.         if (contentType != null)  
  13.             httpPost.setHeader("Content-type", contentType);  
  14.         CloseableHttpResponse httpResponse = m_HttpClient.execute(httpPost);  
  15.         try {  
  16.             HttpEntity entityResponse = httpResponse.getEntity();  
  17.             int contentLength = (int) entityResponse.getContentLength();  
  18.             if (contentLength <= 0)  
  19.                 throw new IOException("No response");  
  20.             byte[] respBuffer = new byte[contentLength];  
  21.             if (entityResponse.getContent().read(respBuffer) != respBuffer.length)  
  22.                 throw new IOException("Read response buffer error");  
  23.             return respBuffer;  
  24.         } finally {  
  25.             httpResponse.close();  
  26.         }  
  27.     }  
  28.   
  29.     public byte[] post(String url, byte[] bytes) throws IOException {  
  30.         return post(url, bytes, null);  
  31.     }  
  32.   
  33.     public String postXml(String url, String str) throws IOException {  
  34.         byte[] reqBuffer = str.getBytes(Charset.forName("UTF-8"));  
  35.         byte[] respBuffer = post(url, reqBuffer, "application/xml; charset=UTF-8");  
  36.         String resp = new String(respBuffer, Charset.forName("UTF-8"));  
  37.         return resp;  
  38.     }  
  39. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值