wood burning stoves 2.0*
The moose likes Web Services and the fly likes Accessing .NET web service from java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Accessing .NET web service from java" Watch "Accessing .NET web service from java" New topic
Author

Accessing .NET web service from java

Maya Pillai
Ranch Hand

Joined: Jul 17, 2008
Posts: 60
Can anyone explain how to access .NET web service from java?

Please check the below code....is this the cirrect way?

import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import javax.xml.rpc.Call;
public class web
{
public static void main(String args[])
{
try
{
String endPoint ="http://ip/Service.asmx";
Service service = new Service();
Call call2 = (Call)service.createCall();
call2.setTargetEndpointAddress(endPoint);
call2.setOperationName(new QName ("op","HelloWorld"));
}
}catch(Exception pe){
pe.printStackTrace();
}
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41079
    
  43
There's no single "right" way to access a WS. Does the code generate the SOAP that the service expects? If so, then it's correct.


Ping & DNS - my free Android networking tools app
Maya Pillai
Ranch Hand

Joined: Jul 17, 2008
Posts: 60
I have re written the code like this...it throws an exxception...


import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;

import java.net.*;

public class aaa {

public void connect() throws Exception
{
Service service = new Service();
Call call = (Call)service.createCall();

String endpoint = "http://ip/Service.asmx";
call.setTargetEndpointAddress(new URL(endpoint));
call.setOperationName(new QName("ResumeUpload"));
String email = "email@gmail.com";
Integer SID = (Integer)call.invoke(new Object [] {
new String(email)});
System.out.println("Got result : " + SID);
}
public static void main(String [] args)
{
aaa w = new aaa();
try
{
w.connect();
}
catch(Exception e)
{
System.out.println("An error has occurred.");
e.printStackTrace();
}
System.out.println("Done");

}
}

Please find the error log

AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Client
faultSubcode:
faultString: Server did not recognize the value of HTTP Header SOAPAction: .
faultActor:
faultNode:
faultDetail:
{http://xml.apache.org/axis/}stackTrace:Server did not recognize the value of HTTP Header SOAPAction: .
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41079
    
  43
SOAPAction headers are pretty common (and widely required); the documentation of the target WS should mention what its value needs to be. If you dig into the Call and/or Service classes you should find ways of setting HTTP headers.

Why are you using Axis 1, by the way? It's quite old. Any particular reason not to use Axis 2?

If this was my problem I'd use Axis' wsdl2java tool to create Java client code for accessing the WS, though. Much easier than to try to write the code on one's own.
Ammu Isac
Greenhorn

Joined: Dec 04, 2009
Posts: 6
Hi,
I am a newbie to web services & I too facing the same issue m using axis1 and java 1.4 since its an existing application we are not allowed to change the versions.

I need to call a .net web service from java,

I tried the below code but it doesn't work,with this code I could call a java webservice but not the .Net service.

//String endPoint ="http://ip/axis/services";
String endPoint = "http://ip/Service.asmx";

Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endPoint));
System.out.println(call.getOperationName());

//call.setOperationName(new QName("Login_ws","getEmployees"));
call.setOperationName(new QName("ServiceSoap","HelloWorld"));

String ans = (String) call.invoke(new Object []{});
System.out.println("Result From webService : " + ans);


FaultString: Server did not recognize the value of HTTP Header SOAPAction:


Can anybody provide a sample code...


Thanks.
Ammu Isac
Greenhorn

Joined: Dec 04, 2009
Posts: 6

Hi...I have fixed this issue..Please see my code

import java.net.MalformedURLException;
import java.rmi.RemoteException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;
public class ddd
{
public static void main(String args[]) throws ServiceException, MalformedURLException, RemoteException
{
try
{
String endPoint ="http://ip/webservice/Service.asmx";
//String endPoint ="http://ip/axis/services";
Service service = new Service();
Call call = (Call) service.createCall();
String mailId = "World";
call.setTargetEndpointAddress(new java.net.URL(endPoint));
call.addParameter("mailId", XMLType.XSD_STRING, ParameterMode.IN);
call.setSOAPActionURI("mywebservice/WelcomeUser");
call.setOperationName(new QName("mywebservice","WelcomeUser"));
System.out.println(call.getOperationName());
call.setReturnType(XMLType.XSD_STRING);
String ans = (String) call.invoke(new Object []{mailId});
System.out.println("Result From webService : " + ans);
}
catch(Exception e)
{
e.printStackTrace();
}

}
}


It is printing - Result From webService : Hello

Actually the web service is supposed to accept parameter 'World' and return the string 'Hello World'

Why i am not getting the correct out put? can anyone help me?

Is this the correct way to pass parameter to the web service?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing .NET web service from java
 
Similar Threads
AXIS client example: need help.
SaxParseException
problem with perl web services and java client
Soap client giving error
Java client connect to soap server