• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4090
  • Last Modified:

How to make a Java Soap Call...

I have been given instructions to create a soap request with the following information.  I obviuosly have no idea what I'm doing, can someone help me here....  I have attached code which is a "start" but not quite there.  And I have no idea what uri to use??

(SOAP 1.1)
-------------- START --------------------------
POST /htm2pdf.asmx HTTP/1.1
Host: www.htm2pdf.co.uk
Content-Type: text/xml; charset=utf-8
Content-Length: [length]
SOAPAction: "http://htm2pdf.co.uk/Htm2PdfDoc"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Htm2PdfDoc xmlns="http://htm2pdf.co.uk/">
      <html>[string]</html>
      <key>[string]</key>
    </Htm2PdfDoc>
  </soap:Body>
</soap:Envelope>
------------------ END ---------------------

public static void main(String[] args) throws Exception {
		
	String url = "http://www.htm2pdf.co.uk/htm2pdf.asmx";
	String uri = "????";   // WHAT DO I PUT HERE??
	String methodName = "Htm2PdfDoc";
	String uniqueKey = "12345";		
	String htmlText = "<html><body><h1>Hello World!</h1></body></html>";
	
	Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodName);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
 
	Vector params = new Vector();
	params.addElement(new Parameter("html", String.class, htmlText, null));
	params.addElement(new Parameter("key", String.class, uniqueKey, null));
	call.setParams(params);
 
	URL service = new URL(url);
	Response translation = call.invoke(service, "");
 
	Parameter result = null;
	if (translation.generatedFault()) {
		Fault fault = translation.getFault();
		System.err.println("Generated fault: " + fault);
	} else {
		result = translation.getReturnValue();
		System.out.println(result.getValue());
	}
}

Open in new window

0
burtonrhodes
Asked:
burtonrhodes
  • 3
  • 2
  • 2
  • +1
1 Solution
 
objectsCommented:
think the uri should be the name of the service you are calling

0
 
dojolavaCommented:
Yes, it should be the name of the service, something like :

call.setTargetObjectURI("urn:ServiceName");

Open in new window

0
 
burtonrhodesAuthor Commented:
Yes... that is the question isn't it :)  I have no idea what "the name of the service is".  Is this something that I will have to find out from the service developer?  For some reason they didn't tell me in the documentation.  Do I really need to input the uri??  Is it required?

Their test code is not in java but written this way (but I see no uri code here)....

Dim objHtm2Pdf As New htm2pdf.htm2pdf
Dim pdfBytes As Byte() = objHtm2Pdf.Htm2PdfDoc("<p>Some HTML</p>", "<your unique key here >")

Dim objFileStream As System.IO.FileStream
objFileStream = New System.IO.FileStream("c:\aDocument.pdf", System.IO.FileMode.Create)
objFileStream.Write(pdfBytes, 0, pdfBytes.Length)
objFileStream.Close()
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dojolavaCommented:
At http://www.htm2pdf.co.uk/htm2pdf.asmx?WSDL is the service description, I think the service name is htm2pdf. Try htm2pdf or http://www.htm2pdf.co.uk/htm2pdf as URI.
0
 
burtonrhodesAuthor Commented:
I think you may be right with  http://www.htm2pdf.co.uk/htm2pdf   I'll do some testing a bit later today and let you know.
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
First of let me know what framework are u using? you may have to use something like axis and then need to make a direct call similar to the one I have answered here

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/SOAP/Q_23396120.html

Please not you must and should set the SOAPAction header in the http header for proper invocation of your service.
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
ok here is a working axis direct client , try it with some valid key
package com;
 
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
 
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
 
public class DirectClient {
 
	public static void main(String[] args) throws Exception {
        
        String url = "http://www.htm2pdf.co.uk/htm2pdf.asmx";
        String uri = "http://htm2pdf.co.uk/Htm2PdfDoc";   // WHAT DO I PUT HERE??
        String methodName = "Htm2PdfDoc";
        String uniqueKey = "12345";             
        String htmlText = "<html><body><h1>Hello World!</h1></body></html>";
        try {
            String endpoint =  url;
    
            Service  service = new Service();
    
            Call call= (Call) service.createCall();
    
            //call.setProperty( Call.SOAPACTION_USE_PROPERTY, new Boolean( true ) );
    
           // call.setProperty( Call.SOAPACTION_URI_PROPERTY, "http://www.webserviceX.NET/GetQuote");
    
            call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean(true));
            call.setProperty(Call.SOAPACTION_URI_PROPERTY, uri );
            call.setReturnType(XMLType.XSD_STRING);
            
            QName html = new QName("http://htm2pdf.co.uk/","html");
            QName key = new QName("http://htm2pdf.co.uk/","key");
            call.addParameter(html,XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(key,XMLType.XSD_STRING, ParameterMode.IN);
    
            call.setTargetEndpointAddress( new java.net.URL(endpoint) );
    
            call.setOperationName(new QName("http://htm2pdf.co.uk/", methodName) );
//            System.out.println(call.);
    
            Object obj = call.invoke( new Object[] { htmlText , uniqueKey} );
    
           // Exception e = (Exception) obj;
            System.out.println("RESULT:" + obj + "'");
          //  e.printStackTrace();
    
     } catch (Exception e) {
    
            System.err.println(e.toString());
    
            e.printStackTrace();
    
    }
 
}
}

Open in new window

0
 
burtonrhodesAuthor Commented:
Holy crap.  That was the best answer I've ever recieved on Experts-exchange.  Works like charm.  a+++
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now