国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > 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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南雄市| 沙田区| 磐石市| 通榆县| 加查县| 正镶白旗| 饶阳县| 喜德县| 虎林市| 银川市| 周宁县| 宾阳县| 贵德县| 法库县| 七台河市| 随州市| 太湖县| 崇明县| 西昌市| 台前县| 阿拉善盟| 镇安县| 荥阳市| 皮山县| 福鼎市| 淮南市| 南充市| 克山县| 阜阳市| 兰溪市| 通州区| 方山县| 石渠县| 汉沽区| 营口市| 张北县| 麻城市| 日喀则市| 南安市| 马山县| 上饶县|