HttpClient Post 二進制/字節(jié)流/byte[]實例代碼
更新時間:2017年06月19日 11:53:57 投稿:lqh
這篇文章主要介紹了 HttpClient Post 二進制/字節(jié)流/byte[]實例代碼的相關資料,需要的朋友可以參考下
HttpClient Post 二進制/字節(jié)流/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; } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
jsp+ajax實現(xiàn)的局部刷新較驗驗證碼(onblur事件觸發(fā)較驗)
這篇文章主要介紹了jsp+ajax實現(xiàn)的局部刷新較驗驗證碼,基于onblur事件觸發(fā)較驗功能,以實例形式詳細的分析了前臺顯示、圖片生成及Ajax動態(tài)驗證等詳細技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10基于javaweb+jsp實現(xiàn)學生宿舍管理系統(tǒng)
這篇文章主要介紹了基于javaweb+jsp實現(xiàn)的學生宿舍管理系統(tǒng)的示例代碼,文中的代碼介紹詳細,對我們學習JSP有一定的幫助,需要的朋友可以參考一下2021-12-12