使commons httpclient支持https協議類,是commons httpclientjava
import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.SocketFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.params.HttpConnectionParams; import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; /** * httpclient https * */ public class HTTPSSecureProtocolSocketFactory implements ProtocolSocketFactory {//SecureProtocolSocketFactory private SSLContext sslcontext = null; private SSLContext createSSLContext() { SSLContext sslcontext = null; try { sslcontext = SSLContext.getInstance("SSL"); sslcontext.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } return sslcontext; } private SSLContext getSSLContext() { if (this.sslcontext == null) { this.sslcontext = createSSLContext(); } return this.sslcontext; } public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose); } public Socket createSocket(String host, int port) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(host, port); } public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort); } public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } int timeout = params.getConnectionTimeout(); SocketFactory socketfactory = getSSLContext().getSocketFactory(); if (timeout == 0) { return socketfactory.createSocket(host, port, localAddress, localPort); } else { Socket socket = socketfactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.bind(localaddr); socket.connect(remoteaddr, timeout); return socket; } } // 自定義私有類 private static class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[] {}; } } }
httpclient請求類apache
import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.httpclient.protocol.Protocol; public class SoapUtil { /** * <p>Description: 根據請求報文,請求服務地址獲取 響應報文 * @param requestSoap 請求報文 * @param serviceAddress 請求地址 * @param charSet 字符集 utf-8 * @param contentType 類型 text/xml; charset=utf-8 * @return map封裝的 服務器響應參數和返回報文.PS:statusCode :200正常響應。responseSoap:響應報文 * <p>thinking: </p> * * @author */ public static Map<String,Object> responseSoap(String requestSoap,String serviceAddress,String charSet, String contentType){ String responseSoap=""; Map<String,Object> resultmap=new HashMap<String,Object>(); PostMethod postMethod = new PostMethod(serviceAddress); HttpClient httpClient = new HttpClient(); Protocol myhttps = new Protocol("https", new HTTPSSecureProtocolSocketFactory(), 443);//支持https Protocol.registerProtocol("https", myhttps); int statusCode = 0; try { httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("USERNAME", "PASSWORD"));//設置用戶名密碼,若是不須要就忽略這一行 StringRequestEntity entity = new StringRequestEntity(requestSoap,contentType,charSet); postMethod.setRequestEntity(entity); statusCode = httpClient.executeMethod(postMethod); resultmap.put("statusCode", statusCode); } catch (IOException e) { throw new RuntimeException("執行http請求失敗", e); } if (statusCode == 200) { try { responseSoap = postMethod.getResponseBodyAsString(); resultmap.put("responseSoap", responseSoap); } catch (IOException e) { throw new RuntimeException("獲取請求返回報文失敗", e); } } else { throw new RuntimeException("請求失敗:" + statusCode); } return resultmap; } }
xml工具類服務器
import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.xml.sax.InputSource; public class XmlUtil { /** * <p>Description:將字符串類型的XML 轉化成Docunent文檔結構</p> * @param parseStrXml 待轉換的xml 字符串 * @return Document * * @author * @throws JDOMException * @throws IOException */ public static Document strXmlToDocument(String parseStrXml) throws JDOMException, IOException{ StringReader read = new StringReader(parseStrXml); //建立新的輸入源SAX 解析器將使用 InputSource 對象來肯定如何讀取 XML 輸入 InputSource source = new InputSource(read); //建立一個新的SAXBuilder SAXBuilder sb = new SAXBuilder(); // 新創建構造器 Document doc = null; try { doc = sb.build(source); } catch (JDOMException e) { throw e; } catch (IOException e) { throw e; } return doc; } /** * <p>Description: 根據目標節點名獲取值</p> * @param doc 文檔結構 * @param finalNodeName 最終節點名 * @return * * @author */ public static String getValueByElementName(Document doc,String finalNodeName){ Element root = doc.getRootElement(); HashMap<String,Object> map=new HashMap<String,Object>(); //調用getChildAllText方法。獲取目標子節點的值 getChildAllText(doc, root,map); String result=(String)map.get(finalNodeName); return result; } /** * <p>Description: 遞歸得到子節點的值</p> * @param doc 文檔結構 * @param e 節點元素 * @param resultmap 遞歸將值壓入map中 * @return * * @author */ public static Map<String, Object> getChildAllText(Document doc, Element e,Map<String, Object> resultmap) { if (e != null) { if (e.getChildren() != null) // 若是存在子節點 { List<Element> list = e.getChildren(); for (Element el : list) // 循環輸出 { if (el.getChildren().size() > 0) // 若是子節點還存在子節點,則遞歸獲取 { getChildAllText(doc, el, resultmap); } else { resultmap.put(el.getName(), el.getTextTrim()); // 將葉子節點值壓入map } } } } return resultmap; } /** * 獲取某個節點下的全部子節點 * @param doc * @param element * @return */ public static List<Element> getChildToList(Document doc,String element){ Element root = doc.getRootElement(); Element e = root.getChild(element); if(e != null){ //判斷要查找的節點是否存在根節點, return e.getChildren();//存在:返回節點下的全部子節點 }else{ return getChildToList(root, element);//不存在:進入遞歸查詢 } } /** * 遞歸查找節點 * @param root * @param element * @return */ private static List<Element> getChildToList(Element root,String element){ List<Element> list = new ArrayList<>(); List<Element> rootElements = root.getChildren(); for (Element element2 : rootElements) { if(element2.getChild(element)!=null){ return element2.getChild(element).getChildren(); }else{ list = getChildToList(element2, element); if(list.size() != 0) return list; } } return new ArrayList<>(); } }