【 tulaoshi.com - Web开发 】
c# JSON返回格式的WEB SERVICEhttp://www.jb51.net/article/16768.htm
我这里是利用axis创建类包的,首先下载axis的压缩包,然后解压,把axis放到webapp下面。如果可以运行,就OK了。
c#创建的webservice自带的wsdl的,直接可以加上?wsdl访问,然后利用axis生成类:
java -classpath ".;commons-logging.jar;axis.jar;commons-discovery.jar;jaxrpc.jar;wsdl4j.jar;saaj.jar;jaxrpc.jar;mail.jar" org.apache.axis.wsdl.WSDL2Java -o "E:cjjer" http://s:9999/IUsers.asmx?wsdl
在目录E:cjjer下面有产生了一系列的java文件。
然后编译这些java文件,注意,由于这样产生的并没有soapheader的请求,需要自己修改java文件:
打开*SoapStub.java这个文件,定位到 protected org.apache.axis.client.Call createCall() throws java.rmi.RemoteException
这个方法,然后再return之前写上
SOAPHeaderElement head = new SOAPHeaderElement(new PrefixedQName(new javax.xml.namespace.QName("http://www.cjjer.com/webs/", "ReqHeader")));
head.setActor(null);
try{
head.addChildElement("userName").addTextNode("cjjer");
head.addChildElement("password").addTextNode("000000");
head.setMustUnderstand(true);
}catch(Exception e){
System.out.println(" soapheader Exception == ");
e.printStackTrace();
}
_call.addHeader(head);
return _call;
这样,就会加上SoapHeader了。提示类找不到的话请import org.apache.axis.message.*;
最后是编写请求代码了:
import java.io.*;
import java.sql.*;
import java.awt.*;
import javax.xml.namespace.*;
import java.awt.event.*;
import java.net.URL;
import org.apache.axis.client.*;
import com.cjjer.www.webs.*;
public class TestNet {
// main method
public static void main(String args[]) {
System.out.println("我要进来了,嘎嘎");
try{
String endpoint= "http://s:9999/IUsers.asmx";
cjjerLocator locator = new cjjerLocator();//
locator.setMaintainSession(true);
cjjerSoap service = locator.getcjjerSoap();//获得服务对象
Member objM= service.getUser(1);
System.out.println(objM.getName());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
如果不发生异常,就OK了,发生异常的话自行处理。
编译:
javac -cp "E:tomcat6webappsaxisWEB-INFlibcommons-logging.jar;E:tomcat6webappsaxisWEB-INFlibaxis.jar;E:tomcat6webappsaxisWEB-INFlibcommons-discovery.jar;E:tomcat6webappsaxisWEB-INFlibjaxrpc.jar;E:tomcat6webappsaxisWEB-INFlibwsdl4j.jar;E:tomcat6webappsaxisWEB-INFlibsaaj.jar;E:tomcat6webappsaxisWEB-INFlibjaxrpc.jar;E:tomcat6webappsaxisWEB-INFlibmail.jar;" com/cjjer/www/webs/*.java -Xlint:unchecked
运行测试:
java -cp "E:tomcat6webappsaxisWEB-INFlibcommons-logging.jar;E:tomcat6webappsaxisWEB-INFlibaxis.jar;E:tomcat6webappsaxisWEB-INFlibcommons-discovery.jar;E:tomcat6webappsaxisWEB-INFlibjaxrpc.jar;E:tomcat6webappsaxisWEB-INFlibwsdl4j.jar;E:tomcat6webappsaxisWEB-INFlibsaaj.jar;E:tomcat6webappsaxisWEB-INFlibjaxrpc.jar;E:tomcat6webappsaxisWEB-INFlibmail.jar;" TestNet