主頁 > 知識(shí)庫 > HttpClient Post 二進(jìn)制/字節(jié)流/byte[]實(shí)例代碼

HttpClient Post 二進(jìn)制/字節(jié)流/byte[]實(shí)例代碼

熱門標(biāo)簽:阿里云 科大訊飛語音識(shí)別系統(tǒng) Linux服務(wù)器 Mysql連接數(shù)設(shè)置 銀行業(yè)務(wù) 電子圍欄 服務(wù)器配置 團(tuán)購網(wǎng)站

 HttpClient Post 二進(jìn)制/字節(jié)流/byte[]實(shí)例代碼

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

 感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

您可能感興趣的文章:
  • Java使用HttpClient實(shí)現(xiàn)Post請(qǐng)求實(shí)例
  • httpclient模擬post請(qǐng)求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法
  • JAVA利用HttpClient進(jìn)行POST請(qǐng)求(HTTPS)實(shí)例
  • Java利用HttpClient模擬POST表單操作應(yīng)用及注意事項(xiàng)
  • java使用httpclient模擬post請(qǐng)求和get請(qǐng)求示例
  • java使用httpclient發(fā)送post請(qǐng)求示例
  • Android HttpClient GET或者POST請(qǐng)求基本使用方法

標(biāo)簽:廣元 蚌埠 大理 萍鄉(xiāng) 衢州 衡水 江蘇 棗莊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《HttpClient Post 二進(jìn)制/字節(jié)流/byte[]實(shí)例代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266