麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JSP > 正文

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

2024-09-05 00:23:08
字體:
來源:轉載
供稿:網友

 java/221602.html">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;   } } 

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


注:相關教程知識閱讀請移步到JSP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91成人一区二区三区 | 成年人精品视频 | 高颜值美女啪啪 | 久久免费观看一级毛片 | 欧美成人综合视频 | 亚洲精品久久久久久下一站 | 国产精品久久二区 | 久色婷婷| 国产一区二区不卡视频 | 免费观看三级毛片 | 懂色av懂色aⅴ精彩av | 毛片在线视频在线播放 | 国产一国产一级毛片视频在线 | 毛片视频免费观看 | 欧美成人午夜一区二区三区 | h色网站免费观看 | 午夜亚洲视频 | 国产精品视频 | 国产精品视频专区 | 欧美精品国产综合久久 | 久久人人爽人人爽人人片av高请 | 久久91亚洲精品久久91综合 | 国产精品久久久久一区二区 | 亚洲片在线观看 | 久久一级 | 精品国产一二区 | 免费三级大片 | 7777在线观看 | 黄色视屏免费看 | 精品一区二区久久久久久按摩 | 久草在线看片 | 日本aaaa片毛片免费观看视频 | 在线成人免费观看视频 | 免费毛片免费看 | 免费国产wwwwwww网站 | 国av在线 | av在线免费观看网 | 亚洲国产高清自拍 | 4p嗯啊巨肉寝室调教男男视频 | 黄视频免费在线 | 7777在线视频免费播放 |