Web service of Java soapheader

Time:2021-8-15

C# JSON return format Web Servicehttps://www.jb51.net/article/16768.htm
I use axis to create a class package here. First download the compressed package of axis, then unzip it and put axis under webapp. If it can run, it’s OK.
   C# create the WSDL of the WebService, which can be added directly? WSDL access, and then use axis to generate classes:

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

A series of java files are generated under the directory e: \ cjjer.

   Then compile these java files. Note that since there is no soap header generated in this way, you need to modify the java files yourself:

   Open the * soapstub.java file and navigate to

    protected org.apache.axis.client.Call createCall() throws java.rmi.RemoteException 

This method, and then write it before 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;

In this way, soapheader will be added. If the prompt class cannot be found, please import   org.apache.axis.message.*;

Finally, write the request code:

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 (“I’m coming in, Gaga”);  
    try{
        String endpoint= “http://s:9999/IUsers.asmx”;
        cjjerLocator locator = new  cjjerLocator();//
        locator.setMaintainSession(true);
         cjjerSoap   service  =  locator.getcjjerSoap();// Get service object
        Member objM= service.getUser(1);
        System.out.println(objM.getName());
        }
        catch (Exception e) {
            e.printStackTrace();

        }


If no exception occurs, it is OK. If an exception occurs, handle it by yourself.
compile:

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

Run test:

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