java调用net_Java调用C#.net开发的WebService
1.開發(fā)C#.net的WebService服務(wù)
1.1點(diǎn)擊“開始”->“程序”-> "Microsoft Visual Studio 2005" -> "Microsoft Visual Studio 2005",打開.net界面
1.2 選擇“文件”-> “新建” -> “網(wǎng)站”,選擇“ASP.NET Web服務(wù)”,命名為WebServiceHelloWord,“確定”;
1.3 WebService工程建好后,能看到默認(rèn)的HelloWord方法
[WebMethod]
public string HelloWorld(string) {
return "Hello World ";
}
修改此方法為:
[WebMethod]
[SoapDocumentMethodAttribute(Action = "http://microsoft.com/webservices/HelloWorld", RequestNamespace = "http://microsoft.com/webservices/T", ResponseNamespace = "http://microsoft.com/webservices/T", ResponseElementName = "arithmeticMeanResponse", Use = SoapBindingUse.Literal)]
public string HelloWorld(string name) {
return "Hello World "? + name;
}
增加參數(shù)name和SoapDocumentMethodAttribute設(shè)置,注意SoapDocumentMethodAttribute必須設(shè)置,否則java調(diào)用C#.net時會調(diào)用不成功。
1.4 編譯并部署到服務(wù)器的IIS上,命名的名字為websh,地址為http://localhost/websh/Service.asmx
2.開發(fā)java客戶端
2.1 新建java工程WbeServiceTest,新建java類WST,具體代碼如下
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class WST{
/**
* @param args
* @throws ServiceException
* @throws MalformedURLException
* @throws RemoteException
*/
public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException {
//?? TODO Auto-generated method stub
// WebService URL
String service_url = "http://localhost/websh/Service.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(service_url));
// 設(shè)置要調(diào)用的方法
call.setOperationName(new QName("http://microsoft.com/webservices/T", "HelloWorld"));
// 該方法需要的參數(shù)
call.addParameter(new QName("http://microsoft.com/webservices/T","name"), org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
// 方法的返回值類型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://microsoft.com/webservices/HelloWorld");
// 調(diào)用該方法
String res = call.invoke(new Object[] {"rock"}).toString();
System.out.println(" Result: " + res.toString());
}
}
2.2 客戶端開發(fā)完畢,執(zhí)行這個類,就能看到返回的結(jié)果: Result: Hello World rock 表明調(diào)用成功
在java調(diào)用C#.net開發(fā)的WebService過程中,特別要配置[SoapDocumentMethodAttribute(...)]
總結(jié)
以上是生活随笔為你收集整理的java调用net_Java调用C#.net开发的WebService的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java把以0结尾的字符串_Java中字
- 下一篇: c++ PVOID封装成C#