newbie14 0 Posting Pro

I have a webservice to be accessed which is based on request and response. Below is how my php codes are works perfectly fine.

class fNewRequest {
    function fNewReq($pTK, $pVID) 
    {
        $this->pTK= $pTK;
        $this->pVID= $pVID;
    }
}

/* Initialize webservice with your WSDL */
$client = new SoapClient("http://**.**.**.*/**/**/***/webservice.asmx?WSDL");

/* Set your parameters for the request */
$params = array(
  "pTK" => "asa",
  "pVID" => "1134",
);

/* Invoke webservice method with your parameters, in this case: Function1 */
$response = $client->__soapCall("fNewRequests", array($params));

/* Print webservice response */
var_dump($response);

print_r($params);

I am now trying to do the same via java. Below is my codes.

import java.util.*;
import java.io.*;
import java.net.URLConnection;
import java.net.MalformedURLException;

import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;


public class ws1
{
      public static void ws1()
      {
            try
            {
             SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
             SOAPConnection soapConnection = soapConnectionFactory.createConnection();
             String url = "http://**.**.**.*/**/**/***/webservice.asmx?WSDL";
             SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
             printSOAPResponse(soapResponse);
            }
            catch (Exception e)
            {
                  e.printStackTrace();
                  System.err.println(e.toString());
            }
      }
      private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        String serverURI = "http://**.**.**.*/**/**/***/webservice.asmx?WSDL";

        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("wsdl", serverURI);

        SOAPBody soapBody1 = envelope.getBody();
        SOAPElement soapBodyElemX = soapBody1.addChildElement("fNewRequests", "wsdl");

        SOAPElement soapBodyElem1 = soapBodyElemX.addChildElement("pTK", "wsdl");
        soapBodyElem1.addTextNode("asa");
        SOAPElement soapBodyElem2 = soapBodyElemX.addChildElement("pVID", "wsdl");
        soapBodyElem2.addTextNode("1134");


        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("performRecharge", serverURI + "performRecharge");
        System.out.println("");
        soapMessage.saveChanges();

        System.out.print("Request SOAP Message = ");
        soapMessage.writeTo(System.out);
        System.out.println();
        return soapMessage;
    }

    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
    }
      public static     void main(String[] args)
      {
           ws1();
      }
}

Response SOAP Message = <?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: .
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.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)</faultstring><detail/></soap:Fault></soap:Body></soap:Envelope>

I dont know what value to put for the url and uri ? Any help please ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.