My purpose is to create a Soap UI project with a given wsdl URL, save it and send requests all via Java methods. I a getting org.apache.http.client.ClientProtocolException..... Caused by: org.apache.http.ProtocolException: Content-Length header already present
error when I am trying to submit request by setting username and password for an operation. Find my Java method to send requests.
public void runSoap() throws Exception
{
String projectFile = "SoapUIProjects/TestProjectA-soapui-project.xml";
SoapUI.setSoapUICore(new StandaloneSoapUICore(true));
WsdlProject project = new WsdlProject(projectFile);
int c = project.getInterfaceCount();
System.out.println("The interface count ="+c);
for(int i=0;i<c;i++)
{
WsdlInterface wsdl = (WsdlInterface) project.getInterfaceAt(i);
String soapVersion = wsdl.getSoapVersion().toString();
int opc = wsdl.getOperationCount();
String result="";
for(int j=0;j<opc;j++)
{
WsdlOperation op = wsdl.getOperationAt(j);
String opName = op.getName();
System.out.println("OPERATION:"+opName);
WsdlRequest req = op.getRequestByName("Req_"+soapVersion+"_"+opName);
//Assigning correct u/p to an operation: Generate
if(opName.equals("Generate"))
{
System.out.println("The operation is Generate.");
req.setUsername("u1");//Setting username
req.setPassword("u1");//Setting password
}
WsdlSubmitContext wsdlSubmitContext = new WsdlSubmitContext(req);
WsdlSubmit<?> submit = (WsdlSubmit<?>) req.submit(wsdlSubmitContext, false);
Response response = submit.getResponse();
result = response.getContentAsString();
System.out.println("The result ="+result);
}
}
}
Also refer to the image attached. Can somebody suggest me where am I going wrong and how to resolve this problem?
You need to send the username and password in Basic format: String userAndPassword = String. format("%s:%s",username, password); String basicAuth = new sun.
I got the answer to my question.
I had to set the 'Authorisation Type' explicitly and programmatically and the code to do so is this -
req.setAuthType("Preemptive");
By giving this, the request is being sent and I am not getting any exceptions.
In my search to fix this same error, I found that the solution was to enable "Authenticate Preemptively" option (go into preferences-> http settings and check the 'Authenticate Preemptively' box.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With