2016-01-05 20 views
8

Ho creato un'applicazione java che chiama il servizio web C#. Ecco il codice.Come chiamare un servizio web C# in java?

package callwebserviceadd; 

import java.util.Iterator; 
import javax.xml.soap.*; 
import javax.xml.transform.*; 
import javax.xml.transform.stream.StreamResult; 

import org.w3c.dom.Node; 

public class CallWebServiceAdd { 

public static void main(String[] args) { 
     // TODO code application logic here 
     String a = "10"; 
    String b = "20"; 

    String op = "Addweb"; 
    String urn = "WebService1"; 
    String dest = "http://localhost:1267/WebService1.asmx"; 

    try 
    { 
     SOAPConnectionFactory soapConnFact = SOAPConnectionFactory.newInstance(); 
     SOAPConnection conn = soapConnFact.createConnection(); 

     MessageFactory msgFact = MessageFactory.newInstance(); 
     SOAPMessage msg = msgFact.createMessage(); 

     SOAPPart soapPart = msg.getSOAPPart(); 
     SOAPEnvelope envelop = soapPart.getEnvelope(); 
     envelop.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema"); 
     SOAPBody body = envelop.getBody(); 

     QName bodyName = new QName("http://localhost:1267/","Addweb");    
     SOAPElement bodyElement = body.addBodyElement(bodyName); 
     QName name = new QName("A"); 
     SOAPElement symbol = bodyElement.addChildElement(name); 
     symbol.addTextNode(a); 
     /*SOAPElement bodyelement = body.addChildElement("Addition"); 
     SOAPElement bodyelement1 = bodyelement.addChildElement("a").addTextNode(a); 
     SOAPElement bodyelement2 = bodyelement.addChildElement("b").addTextNode(b); 
     */ 
     MimeHeaders headers = msg.getMimeHeaders(); 
     headers.addHeader("SOAPAction", "http://localhost:1267/WebService1.asmx"); 
     //msg.writeTo(System.out); 
     msg.saveChanges(); 

     SOAPMessage reply = conn.call(msg, dest); 

     soapPart = reply.getSOAPPart(); 
     envelop = soapPart.getEnvelope(); 
     body = envelop.getBody(); 

     Iterator iter = body.getChildElements(); 
     Node resultOuter = ((Node)iter.next()).getFirstChild(); 
     Node result = resultOuter.getFirstChild(); 

     System.out.println("add(" + a + ","+ b + ") = " + result.getNodeValue()); 
     reply.writeTo(System.out); 

     conn.close(); 
     } 
     catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
     } 


    } 

} 

errore

Server did not recognize the value of HTTP Header SOAPAction: http://localhost:1267/WebService1.asmx 

Risultato atteso

add(10,20) = 30 

codice C# webservice

public class WebService1: System.Web.Services.WebService 
{   

    [WebMethod] 
    public int AddProg(int a, int b) 
    { 
     return a + b; 
    } 
} 

XML restituito

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
     <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soap:Body> 
    <soap:Fault> 
     <faultcode> 
     soap:Client 
    </faultcode><faultstring> 

    System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://localhost:1267/WebService1.asmx. 

    at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest() 
    at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message) 
    at System.Web.Services.Protocols.SoapServerProtocol.Initialize() 
    at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response) 
    at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)</faultstring><detail/></soap:Fault></soap:Body></soap:Envelope>BUILD SUCCESSFUL (total time: 1 second) 

Cosa c'è di sbagliato con le intestazioni?

EDIT

WebService1.wsdl

<?xml version="1.0" encoding="utf-8"?> 
<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://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 
    <wsdl:types> 
    <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"> 
     <s:element name="Addweb"> 
     <s:complexType> 
      <s:sequence> 
      <s:element minOccurs="1" maxOccurs="1" name="a" type="s:int" /> 
      <s:element minOccurs="1" maxOccurs="1" name="b" type="s:int" /> 
      </s:sequence> 
     </s:complexType> 
     </s:element> 
     <s:element name="AddwebResponse"> 
     <s:complexType> 
      <s:sequence> 
      <s:element minOccurs="1" maxOccurs="1" name="AddwebResult" type="s:int" /> 
      </s:sequence> 
     </s:complexType> 
     </s:element> 
    </s:schema> 
    </wsdl:types> 
    <wsdl:message name="AddwebSoapIn"> 
    <wsdl:part name="parameters" element="tns:Addweb" /> 
    </wsdl:message> 
    <wsdl:message name="AddwebSoapOut"> 
    <wsdl:part name="parameters" element="tns:AddwebResponse" /> 
    </wsdl:message> 
    <wsdl:portType name="WebService1Soap"> 
    <wsdl:operation name="Addweb"> 
     <wsdl:input message="tns:AddwebSoapIn" /> 
     <wsdl:output message="tns:AddwebSoapOut" /> 
    </wsdl:operation> 
    </wsdl:portType> 
    <wsdl:binding name="WebService1Soap" type="tns:WebService1Soap"> 
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
    <wsdl:operation name="Addweb"> 
     <soap:operation soapAction="http://tempuri.org/Addweb" style="document" /> 
     <wsdl:input> 
     <soap:body use="literal" /> 
     </wsdl:input> 
     <wsdl:output> 
     <soap:body use="literal" /> 
     </wsdl:output> 
    </wsdl:operation> 
    </wsdl:binding> 
    <wsdl:binding name="WebService1Soap12" type="tns:WebService1Soap"> 
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
    <wsdl:operation name="Addweb"> 
     <soap12:operation soapAction="http://tempuri.org/Addweb" style="document" /> 
     <wsdl:input> 
     <soap12:body use="literal" /> 
     </wsdl:input> 
     <wsdl:output> 
     <soap12:body use="literal" /> 
     </wsdl:output> 
    </wsdl:operation> 
    </wsdl:binding> 
    <wsdl:service name="WebService1"> 
    <wsdl:port name="WebService1Soap" binding="tns:WebService1Soap"> 
     <soap:address location="http://localhost:1267/WebService1.asmx" /> 
    </wsdl:port> 
    <wsdl:port name="WebService1Soap12" binding="tns:WebService1Soap12"> 
     <soap12:address location="http://localhost:1267/WebService1.asmx" /> 
    </wsdl:port> 
    </wsdl:service> 
</wsdl:definitions> 
+0

eliminare questa System.out.println (e.getMessage()) anziché utilizzare logger o almeno utilizzare e.printStacktrace() che pila la traccia sarebbe più dettagliata. –

+0

Potresti essere in grado di ottenere risposte migliori se hai dato il wsdl del tuo servizio C# piuttosto che il suo codice, specialmente dal momento che non hai taggato la domanda C#. – Aaron

+0

@Aaron, ho creato un client di servizio Web C# per chiamare il servizio Web C#, funziona correttamente, ma non con il client di servizio java –

risposta

2

Il soapAction si specifica nel codice Java headers.addHeader("SOAPAction", "http://localhost:1267/WebService1.asmx"); deve corrispondere al soapAction specificato per l'operazione che stai targeting, cioè http://tempuri.org/HelloWorld o http://tempuri.org/Addweb.

+0

L'errore è sparito !! Grazie, ma non so ancora come posso inviare i valori a & b al servizio web da aggiungere !! –

+0

@HarshitShrivastava Non ho mai creato i messaggi SOAP da zero, ma ho il sospetto che MessageFactory dovrebbe conoscere i WSDL del servizio in modo da poterti aiutare nella creazione di messaggi SOAP per questo servizio, fornendo aeb invece di creare l'intero messaggio XML . – Aaron

+0

@HarshitShrivastava Penso che sarebbe meglio leggere alcuni tutorial Java/SOAP, sarebbe sicuramente di grande aiuto – Aaron

2

Il valore dell'intestazione SOAPAction è errato. Il valore corretto deve essere indicato nel WSDL per ogni operazione. Ad esempio http://tempuri.org/HelloWorld per l'operazione HelloWorld

<wsdl:operation name="HelloWorld"> 
<soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" /> 
<wsdl:input> 

o http://tempuri.org/Addweb per l'operazione Addweb

Problemi correlati