HTTP方式Post通信发送报文

/**
     * 

描述: 使用HttpPost传输 有用户名密码验证的xml格式的WebService调用

* * @param url webservice的URL * @param xmlStr xml格式的数据字符串 * @return 返回传输响应的结果字符串 */ public static String postXMLAuthWithHttpPost(String url, String xmlStr) { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpEntity entity = null; HttpEntity Rentity = null; String retStr = ""; CloseableHttpResponse response = null; try { entity = new StringEntity(xmlStr, "UTF-8"); HttpPost hp = new HttpPost(url); hp.addHeader("Content-Type", "application/soap+xml;charset=UTF-8"); hp.setEntity(entity); response = httpclient.execute(hp); Rentity = response.getEntity(); if (Rentity != null) { retStr = EntityUtils.toString(Rentity, "UTF-8"); } } catch (Exception e) { } finally { // 关闭连接,释放资源 try { if (response != null) { response.close(); } if (httpclient != null) { httpclient.close(); } } catch (IOException e) { } } return retStr; }

 

你可能感兴趣的:(通信)