java调用.net Webservice
2009-10-27 10:23
423 查看
一、.net Server端
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Services.Description;
namespace WebApplication1
{
/// <summary>
/// Example1 的摘要说明
/// </summary>
/*[WebService(Namespace = "urn:www.routon.com.cn")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]*/
[WebService(Namespace = "http://www.haituns.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Example1 : System.Web.Services.WebService
{
/*[SoapDocumentMethodAttribute(
Action = "",
RequestNamespace = "urn:www.routon.com.cn",
RequestElementName = "sayHello",
ResponseNamespace = "urn:www.routon.com.cn",
ResponseElementName = "arithmeticMeanResponse",
Use = SoapBindingUse.Encoded)
]*/
[SoapRpcMethod(Use = SoapBindingUse.Literal, RequestNamespace = "http://www.haituns.com", ResponseNamespace = "http://www.haituns.com")]
[WebMethod]
public string sayHello(string name)
{
return "Hello " + name;
}
}
}
二、java Client端
需要引入axis.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
mail.jar
saaj.jar
wsdl4j-1.5.1.jar
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class Main {
public static void main(String args[]) {
System.out.println("测试SOAP开始");
Service service = new Service();
Call call = null;
try {
call = (Call) service.createCall();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name = "good!";
// String endpoint = "http://www.haituns.net/test/hello.asmx";
String endpoint = "http://127.0.0.1:2370/Example1.asmx";
String nameSpace = "http://www.haituns.com";
// String nameSpace = "http://CAIE.EAI.ExpenseConstract.WebServices";
try {
call.setTargetEndpointAddress(new java.net.URL(endpoint));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
call.setOperationName(new QName(nameSpace, "sayHello"));
// call.addParameter(new QName(nameSpace, "name"),
// org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("name", org.apache.axis.encoding.XMLType.XSD_STRING,ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI(nameSpace+"/sayHello");
String res = "";
try {
res = (String) call.invoke(new Object[] {name});
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("返回值=" + res);
System.out.println("测试SOAP结束");
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Services.Description;
namespace WebApplication1
{
/// <summary>
/// Example1 的摘要说明
/// </summary>
/*[WebService(Namespace = "urn:www.routon.com.cn")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]*/
[WebService(Namespace = "http://www.haituns.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Example1 : System.Web.Services.WebService
{
/*[SoapDocumentMethodAttribute(
Action = "",
RequestNamespace = "urn:www.routon.com.cn",
RequestElementName = "sayHello",
ResponseNamespace = "urn:www.routon.com.cn",
ResponseElementName = "arithmeticMeanResponse",
Use = SoapBindingUse.Encoded)
]*/
[SoapRpcMethod(Use = SoapBindingUse.Literal, RequestNamespace = "http://www.haituns.com", ResponseNamespace = "http://www.haituns.com")]
[WebMethod]
public string sayHello(string name)
{
return "Hello " + name;
}
}
}
二、java Client端
需要引入axis.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
mail.jar
saaj.jar
wsdl4j-1.5.1.jar
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class Main {
public static void main(String args[]) {
System.out.println("测试SOAP开始");
Service service = new Service();
Call call = null;
try {
call = (Call) service.createCall();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name = "good!";
// String endpoint = "http://www.haituns.net/test/hello.asmx";
String endpoint = "http://127.0.0.1:2370/Example1.asmx";
String nameSpace = "http://www.haituns.com";
// String nameSpace = "http://CAIE.EAI.ExpenseConstract.WebServices";
try {
call.setTargetEndpointAddress(new java.net.URL(endpoint));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
call.setOperationName(new QName(nameSpace, "sayHello"));
// call.addParameter(new QName(nameSpace, "name"),
// org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("name", org.apache.axis.encoding.XMLType.XSD_STRING,ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI(nameSpace+"/sayHello");
String res = "";
try {
res = (String) call.invoke(new Object[] {name});
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("返回值=" + res);
System.out.println("测试SOAP结束");
}
}
相关文章推荐
- java调用.net的webservice接口
- java利用axis2调用.net写的webservice,传递自定义的实体类参数
- Java调用.NET webservice方法的几种方式
- [z] .net与java建立WebService再互相调用
- .NET调用Java写的WebService[原]
- Java通过axis调用.NET WebService
- .NET调用Java写的WebService
- 简单的随笔 ,WSDL工具,Oracle备份还原,java调用.net webservice
- java调用带有header信息的.net webService
- java调用.net webservice
- Java调用.NET的WebService
- .net调用java编写的webservice
- Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法 转
- Java调用.net的WebService
- Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法
- Java调用.net webservice
- .NET调用JAVA的WebService方法
- .net与java 使用自定义对象通过WebService调用
- java调用.net webservice 返回图片
- .net调用java的webservice时服务器MTOM模式报错