Popular Posts
Activate IIS GIZP compress 1. Set metabase editable for editing. 2. Add a new extension for gzip. 3. Edit the file c:\windows\system32\inetsrv\MetaBase.xml Add file... Sublime Text hotkeys 編輯 Ctrl + X 刪除行 Ctrl + Enter 插入下一行 Ctrl + Shift + Enter 插入前一行 Ctrl + Shift + ↑ 往上移動一行 C... Tomcat GET encoding edit server.xml : <Connector     URIEncoding="UTF-8"     connectionTimeout="20000"     port="8080"     prot...
Stats
Showing posts with label web service. Show all posts
Showing posts with label web service. Show all posts
ksoap sample
soap 1.1
POST /WebServiceForTest.asmx HTTP/1.1
Host: brucetsai.test
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Concat"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AuthHeader xmlns="http://tempuri.org/">
      <Token1>string</Token1>
    </AuthHeader>
  </soap:Header>
  <soap:Body>
    <Concat xmlns="http://tempuri.org/">
      <key>string</key>
      <value>string</value>
    </Concat>
  </soap:Body>
</soap:Envelope>
soap 1.2
POST /WebServiceForTest.asmx HTTP/1.1
Host: brucetsai.test
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Header>
    <AuthHeader xmlns="http://tempuri.org/">
      <Token1>string</Token1>
    </AuthHeader>
  </soap12:Header>
  <soap12:Body>
    <Concat xmlns="http://tempuri.org/">
      <key>string</key>
      <value>string</value>
    </Concat>
  </soap12:Body>
</soap12:Envelope>
Java code :
import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Element;
import org.xmlpull.v1.XmlPullParserException;

public class TestKSOAP {

    public static void main(String[] args) throws IOException, XmlPullParserException {
        String namespace = "http://tempuri.org/";
        String wsdl = "http://brucetsai.test/WebServiceForTest.asmx?WSDL";
        String method = "Concat";
        String soapAction = "http://tempuri.org/Concat";

        SoapObject soap = new SoapObject(namespace, method);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        
        Element[] headers = new Element[1];
        
        Element AuthHeader = new Element();
        AuthHeader.setName("AuthHeader");
        AuthHeader.setNamespace(namespace);
        
        Element element = new Element();
        element.setName("Token1");
        element.setNamespace(namespace);        
        element.addChild(Element.TEXT, "value of token1");        
        AuthHeader.addChild(Element.ELEMENT, element);
        
        headers[0] = AuthHeader;
        
        soap.addProperty("key", "keyValue");
        soap.addProperty("value", "values");
        
        envelope.setOutputSoapObject(soap);
        envelope.headerOut = headers;
        envelope.bodyOut = soap;
        
        
        HttpTransportSE transport = new HttpTransportSE(wsdl);
        transport.call(soapAction, envelope);
        
        System.out.println(envelope.getResponse());
    }
}
Append soap header into axis
When axis generate classese of web service, it won't include the request of soap header.
Here is a way to append soap header manually into class.
1. Find the ****stub.java file, and move selection to the method
protected org.apache.axis.client.Call createCall() throws java.rmi.RemoteException {}
2. Append code like below before return call.
// 加入 soap header
SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("http://cp.gov.tw/gsp2", "AuthHeader");
soapHeaderElement.setPrefix("");
try {
    soapHeaderElement.addChildElement("Token1").setValue("value of token1");
    soapHeaderElement.addChildElement("ServiceID").setValue("value of serviceid");
} catch (SOAPException e) {
    e.printStackTrace();
}
_call.addHeader(soapHeaderElement);