Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a c# web service in java?

Tags:

java

c#

I have made a java application which calls the C# web service. Here is the code.

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());
        }


    }

}

Error

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

Expected Result

add(10,20) = 30

c# webservice code

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

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

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

What is wrong with the headers ?

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>
like image 644
Harshit Avatar asked Jan 05 '16 09:01

Harshit


People also ask

How do you call C in C++?

Just declare the C function extern "C" (in your C++ code) and call it (from your C or C++ code). For example: // C++ code. extern "C" void f(int); // one way.

How do you call C from R?

The most basic method for calling C code from R is to use the . C() function described in the System and foreign language interfaces section of the Writing R Extensions manual. Other methods exist including the . Call() and .

What is C function in Python?

The C function always has two arguments, conventionally named self and args. The self argument points to the module object for module-level functions; for a method it would point to the object instance. The args argument will be a pointer to a Python tuple object containing the arguments.


1 Answers

The soapAction you specify in your java code headers.addHeader("SOAPAction", "http://localhost:1267/WebService1.asmx"); should correspond to the soapAction specified for the operation you're targeting, that is http://tempuri.org/HelloWorld or http://tempuri.org/Addweb.

like image 52
Aaron Avatar answered Nov 14 '22 22:11

Aaron