如何在java中调用c#web服务

本文关键字:调用 c#web 服务 java | 更新日期: 2023-09-27 18:28:57

我制作了一个调用C#web服务的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());
        }

    }
}

错误

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

预期结果

add(10,20) = 30

c#Web服务代码

public class WebService1: System.Web.Services.WebService   
{           
    [WebMethod]  
    public int AddProg(int a, int b)  
    {  
         return a + b;  
    }  
}

返回的XML

<?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)

标头出了什么问题?

编辑

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>

如何在java中调用c#web服务

在java代码headers.addHeader("SOAPAction", "http://localhost:1267/WebService1.asmx");中指定的soapAction应与为目标操作(即http://tempuri.org/HelloWorldhttp://tempuri.org/Addweb)指定的soapaAction相对应。

SOAPAction标头的值错误。应该在WSDL中为每个操作提供正确的值。例如http://tempuri.org/HelloWorld用于HelloWorld操作

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

或CCD_ 7用于CCD_ 8操作