成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

使用commons httpclient請(qǐng)求https協(xié)議的webservice

VPointer / 2110人閱讀

摘要:使支持協(xié)議類,是自定義私有類請(qǐng)求類根據(jù)請(qǐng)求報(bào)文,請(qǐng)求服務(wù)地址獲取響應(yīng)報(bào)文請(qǐng)求報(bào)文請(qǐng)求地址字符集類型封裝的服務(wù)器響應(yīng)參數(shù)和返回報(bào)文正常響應(yīng)。

使commons httpclient支持https協(xié)議類,是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請(qǐng)求類

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: 根據(jù)請(qǐng)求報(bào)文,請(qǐng)求服務(wù)地址獲取 響應(yīng)報(bào)文 * @param requestSoap 請(qǐng)求報(bào)文 * @param serviceAddress 請(qǐng)求地址 * @param charSet 字符集 utf-8 * @param contentType 類型 text/xml; charset=utf-8 * @return map封裝的 服務(wù)器響應(yīng)參數(shù)和返回報(bào)文.PS:statusCode :200正常響應(yīng)。responseSoap:響應(yīng)報(bào)文 *

thinking:

* * @author */ public static Map responseSoap(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"));//設(shè)置用戶名密碼,如果不需要就忽略這一行 StringRequestEntity entity = new StringRequestEntity(requestSoap,contentType,charSet); postMethod.setRequestEntity(entity); statusCode = httpClient.executeMethod(postMethod); resultmap.put("statusCode", statusCode); } catch (IOException e) { throw new RuntimeException("執(zhí)行http請(qǐng)求失敗", e); } if (statusCode == 200) { try { responseSoap = postMethod.getResponseBodyAsString(); resultmap.put("responseSoap", responseSoap); } catch (IOException e) { throw new RuntimeException("獲取請(qǐng)求返回報(bào)文失敗", e); } } else { throw new RuntimeException("請(qǐng)求失?。? + 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 轉(zhuǎn)化成Docunent文檔結(jié)構(gòu)

* @param parseStrXml 待轉(zhuǎn)換的xml 字符串 * @return Document * * @author * @throws JDOMException * @throws IOException */ public static Document strXmlToDocument(String parseStrXml) throws JDOMException, IOException{ StringReader read = new StringReader(parseStrXml); //創(chuàng)建新的輸入源SAX 解析器將使用 InputSource 對(duì)象來確定如何讀取 XML 輸入 InputSource source = new InputSource(read); //創(chuàng)建一個(gè)新的SAXBuilder SAXBuilder sb = new SAXBuilder(); // 新建立構(gòu)造器 Document doc = null; try { doc = sb.build(source); } catch (JDOMException e) { throw e; } catch (IOException e) { throw e; } return doc; } /** *

Description: 根據(jù)目標(biāo)節(jié)點(diǎn)名獲取值

* @param doc 文檔結(jié)構(gòu) * @param finalNodeName 最終節(jié)點(diǎn)名 * @return * * @author */ public static String getValueByElementName(Document doc,String finalNodeName){ Element root = doc.getRootElement(); HashMap map=new HashMap(); //調(diào)用getChildAllText方法。獲取目標(biāo)子節(jié)點(diǎn)的值 getChildAllText(doc, root,map); String result=(String)map.get(finalNodeName); return result; } /** *

Description: 遞歸獲得子節(jié)點(diǎn)的值

* @param doc 文檔結(jié)構(gòu) * @param e 節(jié)點(diǎn)元素 * @param resultmap 遞歸將值壓入map中 * @return * * @author */ public static Map getChildAllText(Document doc, Element e,Map resultmap) { if (e != null) { if (e.getChildren() != null) // 如果存在子節(jié)點(diǎn) { List list = e.getChildren(); for (Element el : list) // 循環(huán)輸出 { if (el.getChildren().size() > 0) // 如果子節(jié)點(diǎn)還存在子節(jié)點(diǎn),則遞歸獲取 { getChildAllText(doc, el, resultmap); } else { resultmap.put(el.getName(), el.getTextTrim()); // 將葉子節(jié)點(diǎn)值壓入map } } } } return resultmap; } /** * 獲取某個(gè)節(jié)點(diǎn)下的所有子節(jié)點(diǎn) * @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){ //判斷要查找的節(jié)點(diǎn)是否存在根節(jié)點(diǎn), return e.getChildren();//存在:返回節(jié)點(diǎn)下的所有子節(jié)點(diǎn) }else{ return getChildToList(root, element);//不存在:進(jìn)入遞歸查詢 } } /** * 遞歸查找節(jié)點(diǎn) * @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<>(); } }

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/67942.html

相關(guān)文章

  • 構(gòu)建https協(xié)議webService使用httpClient接口訪問

    摘要:組件版本信息使用自帶的命令生成文件命令將拷貝到目錄下配置的目錄文件,在配置文件中新增配置將工程添加進(jìn)并啟動(dòng),使用訪問和鏈接。原理后續(xù)進(jìn)一步研究 1.組件版本信息apache-tomcat-7.0.75JDK 1.8.0_91 2.使用jdk自帶的keytool命令生成keystore文件test.keystore命令:keytool -genkey -alias test123 -ke...

    Tony_Zby 評(píng)論0 收藏0
  • WebService就是這么簡(jiǎn)單

    摘要:它使用方式,接收和響應(yīng)外部系統(tǒng)的某種請(qǐng)求?;仡櫸覀?cè)趯W(xué)習(xí)基礎(chǔ)網(wǎng)絡(luò)編程章節(jié)已經(jīng)知道了這么一個(gè)連接了。使用指定名稱的命名空間。名詞簡(jiǎn)單對(duì)象訪問協(xié)議作為一個(gè)基于語言的協(xié)議用于有網(wǎng)上傳輸數(shù)據(jù)。以的根元素出現(xiàn)。代理這么一個(gè)概念就更加清晰了。 WebService介紹 首先我們來談一下為什么需要學(xué)習(xí)webService這樣的一個(gè)技術(shù)吧.... 問題一 如果我們的網(wǎng)站需要提供一個(gè)天氣預(yù)報(bào)這樣一個(gè)需求...

    SwordFly 評(píng)論0 收藏0
  • 互億無線短信發(fā)送

    摘要:最近幾天有機(jī)會(huì)到了一家科技公司,和里面的項(xiàng)目經(jīng)理聊了一下后端技術(shù),他對(duì)我的講了一下需要會(huì)的一些基本技術(shù),其中提到了驗(yàn)證短信發(fā)送,今天我就剛學(xué)習(xí)了驗(yàn)證短信發(fā)送信息,主要運(yùn)用互億無線短信平臺(tái)進(jìn)行發(fā)送直入主題。 最近幾天有機(jī)會(huì)到了一家科技公司,和里面的項(xiàng)目經(jīng)理聊了一下后端技術(shù),他對(duì)我的講了一下需要會(huì)的一些基本技術(shù),其中提到了驗(yàn)證短信發(fā)送,今天我就剛學(xué)習(xí)了驗(yàn)證短信發(fā)送信息,主要運(yùn)用互億無線短信...

    DobbyKim 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<