我想在 java 中使用 apache axis 连接到 web 服务,但我有一些错误的参数,但我不知道是哪个:
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
public class Test_Web_Service
{
public static void main(String [] args) throws Exception {
try {
String endpoint = "http://www.w3schools.com/webservices/tempconvert.asmx";
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://tempuri.org/CelsiusToFahrenheit");
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName(new QName("http://tempuri.org/CelsiusToFahrenheit","CelsiusToFahrenheit"));
String ret = (String) call.invoke( new Object[] {"20"} );
System.out.println("Sent '20', got '" + ret + "'");
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
网络服务链接:http://www.w3schools.com/webservices/tempconvert.asmx
在 ret 变量中,我收到消息错误。这是因为我在QName中有错误的参数吗?
最佳答案
这是由于客户端代码和服务之间的阻抗不匹配。服务器无法解码您的请求,并继续使用默认值处理
你可以试试这个
call.setOperationName(new QName("http://tempuri.org/","CelsiusToFahrenheit"));
call.addParameter(new QName("http://tempuri.org/","Celsius"),XMLType.XSD_STRING,ParameterMode.IN);
String ret = (String) call.invoke( new Object[] {"20"} );
还要注意 namespaceURI 的变化。
关于java - 如何在java中调用axis apache客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4757741/