摘要:使支持協議類,是自定義私有類請求類根據請求報文,請求服務地址獲取響應報文請求報文請求地址字符集類型封裝的服務器響應參數和返回報文正常響應。
使commons httpclient支持https協議類,是commons httpclient
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請求類
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 { /** *Description: 根據請求報文,請求服務地址獲取 響應報文 * @param requestSoap 請求報文 * @param serviceAddress 請求地址 * @param charSet 字符集 utf-8 * @param contentType 類型 text/xml; charset=utf-8 * @return map封裝的 服務器響應參數和返回報文.PS:statusCode :200正常響應。responseSoap:響應報文 *
thinking:
* * @author */ public static MapresponseSoap(String requestSoap,String serviceAddress,String charSet, String contentType){ String responseSoap=""; Map resultmap=new HashMap (); 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 { /** *Description:將字符串類型的XML 轉化成Docunent文檔結構
* @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; } /** *Description: 根據目標節點名獲取值
* @param doc 文檔結構 * @param finalNodeName 最終節點名 * @return * * @author */ public static String getValueByElementName(Document doc,String finalNodeName){ Element root = doc.getRootElement(); HashMapmap=new HashMap (); //調用getChildAllText方法。獲取目標子節點的值 getChildAllText(doc, root,map); String result=(String)map.get(finalNodeName); return result; } /** * Description: 遞歸獲得子節點的值
* @param doc 文檔結構 * @param e 節點元素 * @param resultmap 遞歸將值壓入map中 * @return * * @author */ public static MapgetChildAllText(Document doc, Element e,Map resultmap) { if (e != null) { if (e.getChildren() != null) // 如果存在子節點 { List 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 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 getChildToList(Element root,String element){ List list = new ArrayList<>(); List 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<>(); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/67942.html
摘要:組件版本信息使用自帶的命令生成文件命令將拷貝到目錄下配置的目錄文件,在配置文件中新增配置將工程添加進并啟動,使用訪問和鏈接。原理后續進一步研究 1.組件版本信息apache-tomcat-7.0.75JDK 1.8.0_91 2.使用jdk自帶的keytool命令生成keystore文件test.keystore命令:keytool -genkey -alias test123 -ke...
摘要:它使用方式,接收和響應外部系統的某種請求?;仡櫸覀冊趯W習基礎網絡編程章節已經知道了這么一個連接了。使用指定名稱的命名空間。名詞簡單對象訪問協議作為一個基于語言的協議用于有網上傳輸數據。以的根元素出現。代理這么一個概念就更加清晰了。 WebService介紹 首先我們來談一下為什么需要學習webService這樣的一個技術吧.... 問題一 如果我們的網站需要提供一個天氣預報這樣一個需求...
閱讀 3865·2021-09-23 11:51
閱讀 3057·2021-09-22 15:59
閱讀 856·2021-09-09 11:37
閱讀 2064·2021-09-08 09:45
閱讀 1260·2019-08-30 15:54
閱讀 2056·2019-08-30 15:53
閱讀 485·2019-08-29 12:12
閱讀 3283·2019-08-29 11:15