java SOAPHEADER的web service

來源:互聯網
上載者:User

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:\tomcat6\webapps\axis\WEB-INF\lib\commons-logging.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\axis.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\commons-discovery.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\jaxrpc.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\wsdl4j.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\saaj.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\jaxrpc.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\mail.jar;" com/cjjer/www/webs/*.java -Xlint:unchecked

運行測試:

java -cp "E:\tomcat6\webapps\axis\WEB-INF\lib\commons-logging.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\axis.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\commons-discovery.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\jaxrpc.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\wsdl4j.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\saaj.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\jaxrpc.jar;E:\tomcat6\webapps\axis\WEB-INF\lib\mail.jar;" TestNet

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.