校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > HttpClient Post 二進制/字節流/byte[]實例代碼

HttpClient Post 二進制/字節流/byte[]實例代碼

熱門標簽:自繪地圖標注數據 外呼系統使用方法 電銷機器人免培訓 潤滑油銷售電銷機器人 給地圖標注得傭金 海外圖書館地圖標注點 南通通訊外呼系統產品介紹 電話機器人需要使用網絡嗎 如何看懂地圖標注點

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

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

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

標簽:黃石 內江 樂山 貸款邀約 廣州 大連 南京 銅川

巨人網絡通訊聲明:本文標題《HttpClient Post 二進制/字節流/byte[]實例代碼》,本文關鍵詞  HttpClient,Post,二進制,字,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《HttpClient Post 二進制/字節流/byte[]實例代碼》相關的同類信息!
  • 本頁收集關于HttpClient Post 二進制/字節流/byte[]實例代碼的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 临江市| 和林格尔县| 东海县| 宜黄县| 秦安县| 翼城县| 甘南县| 五莲县| 曲阜市| 卫辉市| 道孚县| 凤台县| 桂平市| 东阳市| 淮滨县| 万全县| 日喀则市| 吉木乃县| 昌邑市| 和田县| 龙岩市| 五寨县| 白城市| 宁安市| 田阳县| 合江县| 喀喇沁旗| 玉山县| 陆河县| 青川县| 福泉市| 文水县| 阿巴嘎旗| 乐昌市| 新建县| 龙山县| 延寿县| 革吉县| 伊通| 湖州市| 遂平县|