Java SOAPConnection调用Https ASMX服务

Java SOAPConnection调用Https ASMX服务,java,web-services,ssl,https,tls1.2,Java,Web Services,Ssl,Https,Tls1.2,下午好 我已经在这上面工作了几个小时了,我已经在谷歌上搜索了好几个小时了,但没有运气。我有一个驻留在https url上的ASMX服务。我可以通过浏览器毫无问题地点击它,并查看web方法和WSDL。我尝试了几种方法,试图通过java连接到端点,但一直收到相同的403禁止错误 这是我目前的代码: public static String getInspectorSchedulesProd(){ //Local Variable Declaration //List<Inspe

下午好

我已经在这上面工作了几个小时了,我已经在谷歌上搜索了好几个小时了,但没有运气。我有一个驻留在https url上的ASMX服务。我可以通过浏览器毫无问题地点击它,并查看web方法和WSDL。我尝试了几种方法,试图通过java连接到端点,但一直收到相同的403禁止错误

这是我目前的代码:

public static String getInspectorSchedulesProd(){
    //Local Variable Declaration
    //List<InspectorSchedule> returnValue = null;
    String returnValue = "";
    //String url = "";
    String content = "";
    javax.xml.soap.SOAPConnectionFactory connectionFactory = null;
    javax.xml.soap.SOAPConnection connection = null;
    javax.xml.soap.SOAPMessage soapResponse = null;
    java.io.ByteArrayOutputStream out = null;

    try{
        javax.net.ssl.HttpsURLConnection httpsConnection = null;

        // Create SSL context and trust all certificates
        javax.net.ssl.SSLContext sslContext = javax.net.ssl.SSLContext.getInstance("SSL");
        //content = "Current SSL Provider: " + sslContext.getProvider().getName() + "\n";

        for(java.security.Provider Item : java.security.Security.getProviders()){
            //content += Item.getName() + "\n";
        }

        javax.net.ssl.TrustManager[] trustAll = new javax.net.ssl.TrustManager[] {new TrustAllCertificates()};

        sslContext.init(null, trustAll, new java.security.SecureRandom());

        // Set trust all certificates context to HttpsURLConnection
        javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

        // Open HTTPS connection
        java.net.URL url = new java.net.URL("https://www.twiddy.com/webservices/rnsportalws1.asmx");
        httpsConnection = (javax.net.ssl.HttpsURLConnection) url.openConnection();

        // Trust all hosts
        httpsConnection.setHostnameVerifier(new TrustAllHosts());

        // Connect
        httpsConnection.connect();

        content = "Content Type: " + httpsConnection.getContentType() + "\nResponse Message:" + httpsConnection.getResponseMessage() + "\nAllow User Interaction:" + httpsConnection.getAllowUserInteraction();

        // Send HTTP SOAP request and get response
        //connection = javax.xml.soap.SOAPConnectionFactory.newInstance().createConnection();

        //soapResponse = connection.call(createGetInspectorsSchedulesSoapMessageProd(), "https://url/portalws1.asmx");

        //Write the results from the request into the output stream
        //soapResponse.writeTo(out);

        //Convert the stream into a string
        //content = new String(out.toByteArray());

        returnValue = content;

        // Close connection
        //connection.close();
        httpsConnection.disconnect();
    }
    catch (Exception ex){
        returnValue = org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(ex);
    }

    return  returnValue;
}

private static class TrustAllCertificates implements javax.net.ssl.X509TrustManager {
    @Override
    public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {

    }

    @Override
    public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
    }

    @Override
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return new java.security.cert.X509Certificate[]{};
    }
}

private static class TrustAllHosts implements javax.net.ssl.HostnameVerifier {
    public boolean verify(String hostname, javax.net.ssl.SSLSession session) {
        return true;
    }
}

private static javax.xml.soap.SOAPMessage createGetInspectorsSchedulesSoapMessageProd() throws Exception{
    //Local Variable Declaration
    String serverURI = "";
    javax.xml.soap.MessageFactory messageFactory = null;
    javax.xml.soap.SOAPMessage soapMessage = null;
    javax.xml.soap.SOAPPart soapPart = null;
    javax.xml.soap.SOAPEnvelope envelope = null;
    javax.xml.soap.SOAPBody soapBody = null;
    javax.xml.soap.SOAPElement soapBodyElem = null;
    javax.xml.soap.SOAPElement soapMethodParam = null;
    javax.xml.soap.MimeHeaders headers = null;

    //Set the Server Uri Namespace
    serverURI = "http://url/PortalWS1";

    //Init the Message Factory
    messageFactory = javax.xml.soap.MessageFactory.newInstance();

    //Init the Message
    soapMessage = messageFactory.createMessage();

    //Init the SOAP-specific portion of a Message
    soapPart = soapMessage.getSOAPPart();

    //Create the envelope that will wrap the message
    envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("PortalWS1Soap", serverURI);

    //Get an instance of the body of the message
    soapBody = envelope.getBody();

    //Add the Soap Method we wish to call
    soapBodyElem = soapBody.addChildElement("GetInspectorScheds", "PortalWS1Soap");

    //Add the Access Key Parameter
    soapMethodParam = soapBodyElem.addChildElement("strAccessKey", "PortalWS1Soap");
    soapMethodParam.addTextNode("SF!435gFW");

    //Get an instance of the http message headers
    headers = soapMessage.getMimeHeaders();

    //Append our soap Action header to the message
    headers.addHeader("soapAction", "https://url/webservices/portalws1.asmx/GetScheds");

    //Save our chnages
    soapMessage.saveChanges();

    return soapMessage;
}
公共静态字符串getInspectorSchedulesProd(){
//局部变量声明
//List returnValue=null;
字符串returnValue=“”;
//字符串url=“”;
字符串内容=”;
javax.xml.soap.SOAPConnectionFactory connectionFactory=null;
javax.xml.soap.SOAPConnection=null;
javax.xml.soap.SOAPMessage soapResponse=null;
java.io.ByteArrayOutputStream out=null;
试一试{
javax.net.ssl.HttpsURLConnection httpsConnection=null;
//创建SSL上下文并信任所有证书
javax.net.ssl.SSLContext SSLContext=javax.net.ssl.SSLContext.getInstance(“ssl”);
//content=“当前SSL提供程序:”+sslContext.getProvider().getName()+“\n”;
for(java.security.Provider项:java.security.security.getProviders()){
//content+=Item.getName()+“\n”;
}
javax.net.ssl.TrustManager[]trustAll=new javax.net.ssl.TrustManager[]{new TrustAllCertificates()};
init(null,trustAll,new java.security.SecureRandom());
//将信任所有证书上下文设置为HttpsURLConnection
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
//打开HTTPS连接
java.net.URL URL=新的java.net.URL(“https://www.twiddy.com/webservices/rnsportalws1.asmx");
httpsConnection=(javax.net.ssl.HttpsURLConnection)url.openConnection();
//信任所有主机
httpsConnection.setHostnameVerifier(新的TrustAllHosts());
//连接
httpsConnection.connect();
content=“content Type:”+httpsConnection.getContentType()+”\n响应消息:“+httpsConnection.getResponseMessage()+”\n下面的用户交互:“+httpsConnection.getAllowUserInteraction()”;
//发送HTTP SOAP请求并获取响应
//connection=javax.xml.soap.SOAPConnectionFactory.newInstance().createConnection();
//soapResponse=connection.call(CreateGetInspectorScheduleSOApMessageProd(),“https://url/portalws1.asmx");
//将请求的结果写入输出流
//soapResponse.writeTo(out);
//将流转换为字符串
//content=新字符串(out.toByteArray());
返回值=内容;
//密切联系
//connection.close();
httpsConnection.disconnect();
}
捕获(例外情况除外){
returnValue=org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(ex);
}
返回值;
}
私有静态类TrustAllCertificates实现javax.net.ssl.X509TrustManager{
@凌驾
public void checkClientTrusted(java.security.cert.X509Certificate[]证书,字符串authType){
}
@凌驾
public void checkServerTrusted(java.security.cert.X509Certificate[]证书,字符串authType){
}
@凌驾
public java.security.cert.X509Certificate[]getAcceptedIssuers(){
返回新的java.security.cert.X509Certificate[]{};
}
}
私有静态类TrustAllHosts实现javax.net.ssl.HostnameVerifier{
公共布尔验证(字符串主机名,javax.net.ssl.SSLSession会话){
返回true;
}
}
私有静态javax.xml.soap.SOAPMessage CreateGetInspectorScheduleSOApMessageProd()引发异常{
//局部变量声明
字符串serverURI=“”;
javax.xml.soap.MessageFactory MessageFactory=null;
javax.xml.soap.SOAPMessage SOAPMessage=null;
javax.xml.soap.SOAPPart SOAPPart=null;
javax.xml.soap.SOAPEnvelope信封=null;
javax.xml.soap.SOAPBody SOAPBody=null;
javax.xml.soap.SOAPElement soapBodyElem=null;
javax.xml.soap.SOAPElement soapMethodParam=null;
javax.xml.soap.MimeHeaders headers=null;
//设置服务器Uri命名空间
服务器URI=”http://url/PortalWS1";
//初始化消息工厂
messageFactory=javax.xml.soap.messageFactory.newInstance();
//初始化消息
soapMessage=messageFactory.createMessage();
//Init消息的SOAP特定部分
soapPart=soapMessage.getSOAPPart();
//创建用于包装邮件的信封
信封=soapPart.getEnvelope();
addNamespaceDeclaration(“PortalWS1Soap”,serverURI);
//获取消息体的实例
soapBody=envelope.getBody();
//添加我们希望调用的Soap方法
soapBodyElem=soapBody.addChildElement(“GetInspectorScheds”、“PortalWS1Soap”);
//添加访问密钥参数
soapMethodParam=soapBodyElem.addChildElement(“strAccessKey”、“PortalWS1Soap”);
soapMethodParam.addTextNode(“SF!435gFW”);
//获取http消息头的实例
headers=soapMessage.getMimeHeaders();
//将soap操作头附加到消息中
headers.addHeader(“soapAction”https://url/webservices/portalws1.asmx/GetScheds");
//拯救我们的技术
soapMessage.saveChanges();
返回消息;
}


提前谢谢大家

我使用
来自AXIS as的HostnameVerifier类

    HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return fias.HostnameVerifier.DEFAULT.verify(hostname, session);
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
    HttpsURLConnection con = (HttpsURLConnection) new URL(url).openConnection();
    con.connect();
和用于发送SOAP的类
创建自己的密钥库和信任库
我通过IP进行连接,并对密钥使用别名(它们在存储中相等)
不要忘记使用debugSystem.setProperty(“javax.net.debug”、“ssl”)
它对我有用
PS:对SOAP还不起作用(
PSS:对于SOAPAction头,我使用了与serverURI类似的uri。我指的是名称空间+操作