步骤:
1、添加axis包:
2、webservices中的代码
/// <summary>
/// BarCodeServices 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class BarCodeServices : System.Web.Services.WebService
{
[WebMethod]
public string GetBarCodeInformation(string BarCode, string UserName, string PassWord)
{
return "XXXXXXX";
}
}
3、Java中的代码:
import java.net.URL;
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 MainClass {
public static void main(String[] args) throws Exception{
WebServicesUsed();
}
//webservices调用例子
public static void WebServicesUsed() throws Exception
{
// 定义方法
String method = "GetBarCodeInformation";
// 定义服务
Service service = new Service();
// 测试1:调用HelloWorld方法,方法没有参数
Call call2 = (Call) service.createCall();
call2.setTargetEndpointAddress(new java.net.URL("http://10.2.32.64:7005/WebServices/BarCodeServices.asmx")); //weibservice地址
call2.setUseSOAPAction(true);
call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string"));
call2.setOperationName(new QName("http://tempuri.org/", method)); //webservice中namespace
call2.setSOAPActionURI("http://tempuri.org/GetBarCodeInformation"); //webservice中namespace的方法
call2.addParameter(new QName("http://tempuri.org/", "BarCode"),XMLType.XSD_STRING, ParameterMode.IN); //参数
call2.addParameter(new QName("http://tempuri.org/", "UserName"),XMLType.XSD_STRING, ParameterMode.IN); //参数
call2.addParameter(new QName("http://tempuri.org/", "PassWord"),XMLType.XSD_STRING, ParameterMode.IN); //参数
String retVal2 = (String) call2.invoke(new Object[] { "8427650000468","SJYB","81e50f63" }); //参数值
System.out.println(retVal2);
}
}
4、输出结果(json格式):
{"barcode":"8427650000468"}