程序员

HttpClient Post 二进制/字节流/byte[]实例代码

作者:admin 2021-04-24 我要评论

HttpClient Post 二进制/字节流/byte[]实例代码 HttpClient 3.x public class HttpHelper { String m_url; HttpClient m_HttpClient; public HttpHelper(String ...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

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

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


本文转载自网络,原文链接:https://m.jb51.net/article/116578.htm

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • HttpClient Post 二进制/字节流/byte[]

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

  • Java 实现 web服务器的简单实例

    Java 实现 web服务器的简单实例

  • 详解直接访问WEB-INF目录下的JSP页面的

    详解直接访问WEB-INF目录下的JSP页面的

  • Struts2中实现web应用的初始化实例详解

    Struts2中实现web应用的初始化实例详解