I'm trying to call a SOAP webservice with credentials username and password, but it's giving an error, I tried in different ways but it's the same output, it's giving this error:
//error
expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <html>@2:44 in java.io.InputStreamReader@54570a0)
No response is returned.
//variables
private static final String NAMESPACE = "http://tempuri.org/";
private static final String METHOD_NAME = "method";
private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
private static final String URL = "https://example.com/and/service.asmx";
private static final String USER_NAME = "username";
private static final String PASSWORD = "password";
//calling web service
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo deviceIdPI = new PropertyInfo();
deviceIdPI.setName("device_id");
deviceIdPI.setValue(deviceId);
deviceIdPI.setType(String.class);
request.addProperty(deviceIdPI);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// add header to envelope
envelope.headerOut = getHeaderElement();
System.out.println("HeaderValue " + Arrays.toString(envelope.headerOut));
envelope.dotNet = false;
envelope.bodyOut = request;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
System.out.println("BodyOut " + envelope.bodyOut.toString());
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
System.out.println("ResponseMessage" + response.toString());
responseMsg = String.valueOf(response);
System.out.println("ResponseMessage" + response.toString());
public static Element[] getHeaderElement() {
// 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");
//username
Element username = new Element().createElement(null, USER_NAME);
username.addChild(Node.IGNORABLE_WHITESPACE,"CBROWN");
header[0].addChild(Node.ELEMENT, username);
//password
Element pass = new Element().createElement(null,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, PASSWORD);
username.addChild(Node.ELEMENT, pass);
return header;
}
}
You can authenticate a Web Service call in two ways:
Authorizationheaders.Both authentication mechanisms are in fact complementary, and the use of one of another depends on the Web Service provider.
In my opinion, the provider of your Web Service expects authentication at the transport level at least and, when you send the SOAP request without the necessary headers, it is returning a
401HTML page, which is causing the error you are describing:According to this related SO question, you can provide the necessary headers with something like this: