title: How to call a c# web service in java?
body: <p>I have made a java application which calls the C# web service. Here is the code.</p>
  
  <pre><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());
          }
  
  
      }
  
  }
  </code></pre>
  
  <p>Error</p>
  
  <pre><code>Server did not recognize the value of HTTP Header SOAPAction: http://localhost:1267/WebService1.asmx
  </code></pre>
  
  <p>Expected Result</p>
  
  <pre><code>add(10,20) = 30
  </code></pre>
  
  <p>c# webservice code</p>
  
  <pre><code>public class WebService1: System.Web.Services.WebService   
  {           
  
      [WebMethod]  
      public int AddProg(int a, int b)  
      {  
           return a + b;  
      }  
  }
  </code></pre>
  
  <p>Returned XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
        &lt;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"&gt;
     &lt;soap:Body&gt;
      &lt;soap:Fault&gt;
        &lt;faultcode&gt;
           soap:Client
       &lt;/faultcode&gt;&lt;faultstring&gt;
  
     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;amp; abortProcessing)&lt;/faultstring&gt;&lt;detail/&gt;&lt;/soap:Fault&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;BUILD SUCCESSFUL (total time: 1 second)
  </code></pre>
  
  <p>What is wrong with the headers ?</p>
  
  <p>EDIT</p>
  
  <p>WebService1.wsdl</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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/"&gt;
    &lt;wsdl:types&gt;
      &lt;s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"&gt;
        &lt;s:element name="Addweb"&gt;
          &lt;s:complexType&gt;
            &lt;s:sequence&gt;
              &lt;s:element minOccurs="1" maxOccurs="1" name="a" type="s:int" /&gt;
              &lt;s:element minOccurs="1" maxOccurs="1" name="b" type="s:int" /&gt;
            &lt;/s:sequence&gt;
          &lt;/s:complexType&gt;
        &lt;/s:element&gt;
        &lt;s:element name="AddwebResponse"&gt;
          &lt;s:complexType&gt;
            &lt;s:sequence&gt;
              &lt;s:element minOccurs="1" maxOccurs="1" name="AddwebResult" type="s:int" /&gt;
            &lt;/s:sequence&gt;
          &lt;/s:complexType&gt;
        &lt;/s:element&gt;
      &lt;/s:schema&gt;
    &lt;/wsdl:types&gt;
    &lt;wsdl:message name="AddwebSoapIn"&gt;
      &lt;wsdl:part name="parameters" element="tns:Addweb" /&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:message name="AddwebSoapOut"&gt;
      &lt;wsdl:part name="parameters" element="tns:AddwebResponse" /&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:portType name="WebService1Soap"&gt;
      &lt;wsdl:operation name="Addweb"&gt;
        &lt;wsdl:input message="tns:AddwebSoapIn" /&gt;
        &lt;wsdl:output message="tns:AddwebSoapOut" /&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:portType&gt;
    &lt;wsdl:binding name="WebService1Soap" type="tns:WebService1Soap"&gt;
      &lt;soap:binding transport="http://schemas.xmlsoap.org/soap/http" /&gt;
      &lt;wsdl:operation name="Addweb"&gt;
        &lt;soap:operation soapAction="http://tempuri.org/Addweb" style="document" /&gt;
        &lt;wsdl:input&gt;
          &lt;soap:body use="literal" /&gt;
        &lt;/wsdl:input&gt;
        &lt;wsdl:output&gt;
          &lt;soap:body use="literal" /&gt;
        &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:binding&gt;
    &lt;wsdl:binding name="WebService1Soap12" type="tns:WebService1Soap"&gt;
      &lt;soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /&gt;
      &lt;wsdl:operation name="Addweb"&gt;
        &lt;soap12:operation soapAction="http://tempuri.org/Addweb" style="document" /&gt;
        &lt;wsdl:input&gt;
          &lt;soap12:body use="literal" /&gt;
        &lt;/wsdl:input&gt;
        &lt;wsdl:output&gt;
          &lt;soap12:body use="literal" /&gt;
        &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:binding&gt;
    &lt;wsdl:service name="WebService1"&gt;
      &lt;wsdl:port name="WebService1Soap" binding="tns:WebService1Soap"&gt;
        &lt;soap:address location="http://localhost:1267/WebService1.asmx" /&gt;
      &lt;/wsdl:port&gt;
      &lt;wsdl:port name="WebService1Soap12" binding="tns:WebService1Soap12"&gt;
        &lt;soap12:address location="http://localhost:1267/WebService1.asmx" /&gt;
      &lt;/wsdl:port&gt;
    &lt;/wsdl:service&gt;
  &lt;/wsdl:definitions&gt;
  </code></pre>