无为清净楼资源网 Design By www.qnjia.com
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; } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月16日
2024年11月16日
- 秀兰玛雅.1999-友情人【大旗】【WAV+CUE】
- 小米.2020-我想在城市里当一个乡下人【滚石】【FLAC分轨】
- 齐豫.2003-THE.UNHEARD.OF.CHYI.3CD【苏活音乐】【WAV+CUE】
- 黄乙玲1986-讲什么山盟海誓[日本东芝版][WAV+CUE]
- 曾庆瑜1991-柔情陷阱[台湾派森东芝版][WAV+CUE]
- 陈建江《享受男声》DTS-ES6.1【WAV】
- 群星《闪光的夏天 第5期》[FLAC/分轨][392.38MB]
- 徐小凤《三洋母带》1:1母盘直刻[WAV+CUE][981M]
- 王菲1995《菲靡靡之音》[香港首版][WAV+CUE][1G]
- 《双城之战》主题小游戏现已上线 扮演金克丝探索秘密基地
- 《霍格沃茨之遗》PS5Pro画面对比:光追性能显著提升
- 《怪猎荒野》PS5Pro主机版对比:B测性能都不稳定
- 黄宝欣.1992-黄宝欣金装精选2CD【HOMERUN】【WAV+CUE】
- 群星.1996-宝丽金流行爆弹精丫宝丽金】【WAV+CUE】
- 杜德伟.2005-独领风骚新歌精选辑3CD【滚石】【WAV+CUE】