a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > JSP > 正文

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

2024-09-05 00:23:08
字體:
供稿:網(wǎng)友

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

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


注:相關(guān)教程知識閱讀請移步到JSP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 精品国产一区av | 在线看h| 一级视频毛片 | 久久av一区二区三区 | 国产亚洲欧美精品永久 | 免费黄色福利视频 | 欧美午夜视频在线观看 | 日日夜夜免费精品视频 | 91精品综合久久久久久五月天 | 一级毛片国产 | 一本久久道 | 免费在线视频精品 | 国产成人午夜 | 亚洲国产精品久久久久久女王 | 99久久国产综合精品女不卡 | av大片在线观看 | 国产精品国产精品国产专区不片 | 伊人欧美在线 | 亚洲精品久久久久久久久 | 国产成人午夜高潮毛片 | 特级淫片裸体免费看 | av免费在线播放 | 免费看91| 91在线播放视频 | 国产在线中文字幕 | 亚洲综合电影 | 中文字幕第31页 | 欧美成人免费一级人片100 | 亚洲国产一二区 | 五月激情综合网 | 久久精品91久久久久久再现 | 91精品久久久久 | 日韩亚洲视频 | 国产区福利 | 国产精品美女久久久久久免费 | 五月婷婷色 | 美女视频黄又黄又免费 | 亚洲毛片在线观看 | 深夜成人小视频 | 亚洲一区二区三区四区五区午夜 | 久久久久国产精品午夜一区 |