今天是第一讲:
Java调用C#-WebService
今天做了一个Java调用C#的Webservice的小例子
1.建立Java WebProject工程
2.建立index.jsp
- < %@ page language = "java" contentType = "text/html; charset=ISO-8859-1"
- pageEncoding = "ISO-8859-1" % >
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- < %@page import = "com.fh.Test" % >
- < html >
- < head >
- < meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" >
- < title > Insert title here </ title >
- </ head >
- < body >
- < form action = "index.jsp" method = "get" name = "testform" >
- < input type = "text" name = "name" size = 10 />
- < input type = "submit" name = "submit" value = "submit" >
- < input type = "reset" name = "reset" value = "reset" />
- </ form >
- < %
- String str = request .getParameter("name");
- System.out.print(str);
- Test pc = new Test();
- pc.Call(str);
- %>
- </ body >
- </ html >
Test.java
- package com.fh;
- public class Test {
- public void Call(String str) throws Exception
- {
- System.out.println(str);
- String result = "" ;
- result = WebServiceUtil.CallWebService(str);
- }
- }
WebServiceUtil.java
- package com.fh;
- import javax.xml.namespace.QName;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- public class WebServiceUtil {
- public static String CallWebService(String name) {
- String res = "" ;
- try {
- String service_url = "<span style=" color: #ff0000;">http: //test.com:81/Service1.asmx</span>
- ";
- Service service = new Service();
- Call call = (Call) service.createCall();
- call.setTargetEndpointAddress(new java.net.URL(service_url));
- call.setOperationName(new QName( "<span style=" color: #ff0000;">http: //test.com/T</span>
- ",
- "<span style=" color: #ff0000;">HelloWordByName</span>
- "));
- call.addParameter("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("<span style=" color: #ff0000;">http: //test.com/HelloWordByName</span>
- ");
- if (name == null ) {
- name = "namepp" ;
- }
- res = (String) call.invoke(new Object[] { name });
- System.out.println(" Result: " + res.toString());
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return res;
- }
- }
注意:我们需要引入axis.jar等几个jar包,请到官方下载。
3.建立C#(WebService)工程
Service1.asmx.cs
- using System;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.Web.Services.Description;
- namespace WebServiceTest
- {
- /// <summary>
- /// Summary description for Service1
- /// </summary>
- [WebService(Namespace = "<span style=" color: #ff0000;">http: //test.com/</span>
- ")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [System.ComponentModel.ToolboxItem(false )]
- // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
- // [System.Web.Script.Services.ScriptService]
- public class Service1 : System.Web.Services.WebService
- {
- [SoapRpcMethod(Action = "<span style=" color: #ff0000;">http: //test.com/HelloWordByName</span>
- ",
- RequestNamespace = "<span style=" color: #ff0000;">http: //test.com/T</span>
- ",
- ResponseNamespace = "<span style=" color: #ff0000;">http: //test.com/T</span>
- ",
- Use = SoapBindingUse.Literal)]
- [WebMethod]
- public string HelloWordByName(String name)
- {
- String hwname = "Hello Word !!" + name;
- return hwname;
- }
- }
- }
注意:要publish发布工程,发布到一个指定的目录,便于IIS引入路径。
4.建立IIS虚拟目录
在IIS上面测试WebService是否可用。
如果大家喜欢?
后续我会带来,第二节课, C#和Flex通过WebOrb交互工程。