简体   繁体   English

无法使用Java客户端访问Asmx Soap Web服务

[英]Not able to access asmx soap web service using java client

I am trying to access the asmx webservice using java: The wsdl is given as below: 我正在尝试使用java访问asmx Web服务:wsdl如下所示:

<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://infotelconnect.com/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://infotelconnect.com/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://infotelconnect.com/">
<s:element name="chk_mob">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="directorynumber" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="email" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="alternate" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="chk_mobResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="chk_mobResult" type="tns:Responsevalid"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="Responsevalid">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="errcd" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="err" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="subaccnum" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="accnum" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="altnum" type="s:string"/>
</s:sequence>
</s:complexType>
<s:element name="AuthHeader" type="tns:AuthHeader"/>
<s:complexType name="AuthHeader">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="Username" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="IP" type="s:string"/>
</s:sequence>
<s:anyAttribute/>
</s:complexType>
</s:schema>
</wsdl:types>
<wsdl:message name="chk_mobSoapIn">
<wsdl:part name="parameters" element="tns:chk_mob"/>
</wsdl:message>
<wsdl:message name="chk_mobSoapOut">
<wsdl:part name="parameters" element="tns:chk_mobResponse"/>
</wsdl:message>
<wsdl:message name="chk_mobAuthHeader">
<wsdl:part name="AuthHeader" element="tns:AuthHeader"/>
</wsdl:message>
<wsdl:portType name="DNvalidationSoap">
<wsdl:operation name="chk_mob">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Validate Directory Number!</wsdl:documentation>
<wsdl:input message="tns:chk_mobSoapIn"/>
<wsdl:output message="tns:chk_mobSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="DNvalidationSoap" type="tns:DNvalidationSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="chk_mob">
<soap:operation soapAction="http://infotelconnect.com/chk_mob" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
<soap:header message="tns:chk_mobAuthHeader" part="AuthHeader" use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="DNvalidationSoap12" type="tns:DNvalidationSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="chk_mob">
<soap12:operation soapAction="http://infotelconnect.com/chk_mob" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
<soap12:header message="tns:chk_mobAuthHeader" part="AuthHeader" use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="DNvalidation">
<wsdl:port name="DNvalidationSoap" binding="tns:DNvalidationSoap">
<soap:address location="https://202.164.33.157:7074/DNvalidation.asmx"/>
</wsdl:port>
<wsdl:port name="DNvalidationSoap12" binding="tns:DNvalidationSoap12">
<soap12:address location="https://202.164.33.157:7074/DNvalidation.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

and my client code generated by netbeans is given as below: NetBeans生成的我的客户代码如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.eclat.client;

import com.sun.xml.ws.security.soap12.Header;
import com.sun.xml.wss.XWSSConstants;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPFactory;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;
import org.eclat.vtlservice.AuthHeader;
import org.eclat.vtlservice.Responsevalid;
import org.eclat.vtlservice.DNvalidation;
import org.eclat.vtlservice.DNvalidationSoap;

/**
 *
 * @author surjit
 */
public class Client {

    private static final String WS_URL = "https://202.164.33.157:7074/DNvalidation.asmx?WSDL";


    private static void trustEveryone() { 
     try { 
             HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){ 
                     public boolean verify(String hostname, SSLSession session) { 
                             return true; 
                     }}); 
             SSLContext context = SSLContext.getInstance("TLS"); 
             context.init(null, new X509TrustManager[]{new 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[0]; 
                     }}}, new SecureRandom()); 
             HttpsURLConnection.setDefaultSSLSocketFactory( 
                             context.getSocketFactory()); 
     } catch (Exception e) { // should never happen 
             e.printStackTrace(); 
     } 
 }

    public static  Responsevalid chkMob(String directorynumber, String email, String alternate) {

        DNvalidation service = new org.eclat.vtlservice.DNvalidation();
        DNvalidationSoap port = service.getDNvalidationSoap();

        Map<String, Object> requestContext = ((BindingProvider)port).getRequestContext();

        requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);
        Map<String, List<String>> requestHeaders = new HashMap<String, List<String>>();
        requestHeaders.put("Username", Collections.singletonList("username"));
        requestHeaders.put("Password", Collections.singletonList("password"));
        requestHeaders.put("IP", Collections.singletonList("IP"));
        requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders);

        return port.chkMob(directorynumber, email, alternate);

    }

    public static void main(String args[]) {  
        trustEveryone();
       System.out.println(chkMob("parameter1","parameter2","parameter3")); 
    }

}


But when I am running this code..I am getting following error:



Jan 16, 2014 5:38:47 PM [com.sun.xml.ws.policy.jaxws.PolicyConfigParser]  parse
INFO: WSP5018: Loaded WSIT configuration from file: file:/E:/New%20Workspace/src/service/build/web/WEB-INF/classes/META-INF/wsit-client.xml.
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at DNvalidation.chk_mob(String directorynumber, String email, String alternate) in d:\QTLAPI\App_Code\DNvalidation.cs:line 57
   --- End of inner exception stack trace ---
    at com.sun.xml.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:189)
    at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:122)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:119)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:89)
    at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:140)
    at com.sun.proxy.$Proxy37.chkMob(Unknown Source)
    at org.eclat.client.Client.chkMob(Client.java:85)`enter code here`
    at org.eclat.client.Client.main(Client.java:91)
Java Result: 1

Please suggest any solution so that I get able to access this web service. 请提出任何解决方案,以便我能够访问此Web服务。

Please refine your code, you are not forming your request message properly for this webservice. 请优化您的代码,因为此Web服务的请求消息格式不正确。 i think this line is causing problem. 我认为这条线引起了问题。

return port.chkMob(directorynumber, email, alternate);

First of all, it's a soap webservice, your request message structure should look like this, 首先,这是一个肥皂网络服务,您的请求消息结构应如下所示,

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:inf="http://infotelconnect.com/">
   <soapenv:Header>
      <inf:AuthHeader>
         <!--Optional:-->
         <inf:Username>?</inf:Username>
         <!--Optional:-->
         <inf:Password>?</inf:Password>
         <!--Optional:-->
         <inf:IP>?</inf:IP>
      </inf:AuthHeader>
   </soapenv:Header>
   <soapenv:Body>
      <inf:chk_mob>
         <!--Optional:-->
         <inf:directorynumber>?</inf:directorynumber>
         <!--Optional:-->
         <inf:email>?</inf:email>
         <!--Optional:-->
         <inf:alternate>?</inf:alternate>
      </inf:chk_mob>
   </soapenv:Body>
</soapenv:Envelope>

Here you are forming header, 在这里,您将形成标题,

Map<String, Object> requestContext = ((BindingProvider)port).getRequestContext();

    requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);
    Map<String, List<String>> requestHeaders = new HashMap<String, List<String>>();
    requestHeaders.put("Username", Collections.singletonList("username"));
    requestHeaders.put("Password", Collections.singletonList("password"));
    requestHeaders.put("IP", Collections.singletonList("IP"));
    requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders);

But while calling webservice operation chk_mob, 但是在调用网络服务操作chk_mob时,

return port.chkMob(directorynumber, email, alternate);

you are simply providing only parameter1, parameter2, parameter3 as string which you are giving in this line from main method, 您只提供了parameter1,parameter2,parameter3作为字符串,这是您在main方法的这一行中提供的,

System.out.println(chkMob("parameter1","parameter2","parameter3"));

First make a proper soap envelope which i have mentioned above and then make webservice call by providng your soap envelope in that call. 首先制作一个我上面已经提到的适当的肥皂信封,然后通过在该调用中提供您的肥皂信封来进行网络服务调用。 You can find plenty of examples and tutorials online for this java client. 您可以在线为此Java客户端找到大量示例和教程。 FYI, i have used soapui to generate that request message structure of yours. 仅供参考,我已经使用soapui来生成您的请求消息结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM