How to Set Soap Header Using Ksoap2 - Android

how to set soap Header using ksoap2 - android

I did that this way:

import org.kxml2.kdom.Element;

then while preparing envelope

soapEnvelope.headerOut = new Element[1];
soapEnvelope.headerOut[0] = buildAuthHeader();
// ...send request...

with

private Element buildAuthHeader() {
Element h = new Element().createElement(NAMESPACE, "AuthHeader");
Element username = new Element().createElement(NAMESPACE, "user");
username.addChild(Node.TEXT, USERNAME);
h.addChild(Node.ELEMENT, username);
Element pass = new Element().createElement(NAMESPACE, "pass");
pass.addChild(Node.TEXT, PASSWORD);
h.addChild(Node.ELEMENT, pass);

return h;
}

obviously, change strings as needed.

Ksoap2 android - pass simple soap header

This is working for me to give the Security Request Header in kSOAP library in Android

SOAP REQ. Header:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cif="http://www.mawarid.ae/linkedCardsSummary/CRM/CIF.xsd">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-14CBAE357AC169AFA614664925178422">
<wsse:Username>XXXXXXXXXXXX</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXXXXXXX</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>

Android Code:

public static Element buildAuthHeader() {
Element headers[] = new Element[1];
headers[0]= new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");
headers[0].setAttribute(null, "mustUnderstand", "1");
Element security=headers[0];

//user token
Element usernametoken = new Element().createElement(security.getNamespace(), "UsernameToken");
usernametoken.setAttribute("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Id", "UsernameToken-14CBAE357AC169AFA614664925178422");

//username
Element username = new Element().createElement(security.getNamespace(), "Username");
username.addChild(Node.TEXT, HttpConstant.REQ_HEADER_USERNAME);
usernametoken.addChild(Node.ELEMENT,username);

// password
Element password = new Element().createElement(security.getNamespace(), "Password");
password.setAttribute(null, "Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
password.addChild(Node.TEXT, HttpConstant.REQ_HEADER_PASSWORD);
usernametoken.addChild(Node.ELEMENT,password);

headers[0].addChild(Node.ELEMENT, usernametoken);

return headers[0];
}

SoapSerializationEnvelope sSerialaEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
sSerialaEnvelop.dotNet = true;
sSerialaEnvelop.headerOut = new Element[1];
sSerialaEnvelop.headerOut[0] = buildAuthHeader(); //// add security request header
sSerialaEnvelop.bodyOut = sObject;
sSerialaEnvelop.setOutputSoapObject(sObject);

create soap envelope with security header in android using ksoap2

I have found my question's answer. I am put answer to useful others.

public class SOAP_WebService extends Activity
{

private final String NAMESPACE = "http://ws.simple/";
private final String URL = "http://10.0.2.2/SimpleWebservice/simple";
private final String SOAP_ACTION = "http://ws.simple/getString";
private final String METHOD_NAME = "getString";

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.soap_webservice);

Button btnClick = (Button) findViewById(R.id.btnClick);
btnClick.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
callWebservice();
}
});
}
public void callWebservice()
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo weightProp =new PropertyInfo();
weightProp.name = "arg0";
weightProp.setValue("rajan");
request.addProperty(weightProp);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

// create header
Element[] header = new Element[1];
header[0] = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","Security");
header[0].setAttribute(null, "mustUnderstand","1");

Element usernametoken = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "UsernameToken");
usernametoken.setAttribute(null, "Id", "UsernameToken-1");
header[0].addChild(Node.ELEMENT,usernametoken);

Element username = new Element().createElement(null, "n0:Username");
username.addChild(Node.IGNORABLE_WHITESPACE,"CBROWN");
usernametoken.addChild(Node.ELEMENT,username);

Element pass = new Element().createElement(null,"n0:Password");
pass.setAttribute(null, "Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
pass.addChild(Node.TEXT, "welcome");

usernametoken.addChild(Node.ELEMENT, pass);

// add header to envelope
envelope.headerOut = header;

Log.i("header", "" + envelope.headerOut.toString());

envelope.dotNet = false;
envelope.bodyOut = request;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
Log.i("bodyout", "" + envelope.bodyOut.toString());

try
{
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);

SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.i("myApp", response.toString());
}
catch (SoapFault e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
Log.d("Exception Generated", ""+e.getMessage());
}

}

}

Android: How to get data from both header and body using KSOAP2

Give this a go all you need to do is put in the correct values and then put it inside an AsyncTask or something like that. The first time i did soap i had a similar problem and it took me quite a while to figure it all out but fingers crossed this should work!

        Element[] header = new Element[1];
header[0] = new Element().createElement(NAMESPACE, "CompanyHeader");

Element accountInfo = new Element().createElement(NAMESPACE, "AccountInfo");
header[0].addChild(Node.ELEMENT, accountInfo);

Element apiKey = new Element().createElement(NAMESPACE, "PartnerID");
apiKey.addChild(Node.TEXT, "String");
accountInfo.addChild(Node.ELEMENT, apiKey);

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

PropertyInfo keyword =new PropertyInfo();
keyword.name = "keyword";
keyword.setValue("string");
request.addProperty(keyword);

PropertyInfo records =new PropertyInfo();
records.name = "records";
records.setValue(int);
request.addProperty(records);

PropertyInfo startingRecord =new PropertyInfo();
startingRecord.name = "startingRecord";
startingRecord.setValue(int);
request.addProperty(startingRecord);

PropertyInfo searchOptions =new PropertyInfo();
searchOptions.name = "searchOptions";
searchOptions.setValue(string);
request.addProperty(searchOptions);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope
.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
envelope.headerOut = header;
envelope.bodyOut = request;

HttpTransportSE ht = getHttpTransportSE();

try {
ht.call(SOAP_ACTION, envelope);
} catch (HttpResponseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} finally {
try {
if (envelope.getResponse() != null) {
SoapObject result = (SoapObject)envelope.getResponse();
Log.e("Results = ", String.valueOf(results));
}
} catch (SoapFault e) {
e.printStackTrace();
}
}

Add header to Http Header

You can retrieve the connection object and add the headers there.

HttpTransportSE transport = new HttpTransportSE(url,timeout);
ServiceConnection conn = transport.getConnection();
conn.setRequestProperty("Accept-Encoding", "utf-8");
//and others...


Related Topics



Leave a reply



Submit