Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClientProtocolException when trying to submit request to a SOAP WsdlRequest by setting username and password via Java

Tags:

java

soapui

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);
        }
    }
}

SoapUI Window

Also refer to the image attached. Can somebody suggest me where am I going wrong and how to resolve this problem?

like image 357
priti Avatar asked Jun 12 '13 12:06

priti


People also ask

How do I send a SOAP request with username and password?

You need to send the username and password in Basic format: String userAndPassword = String. format("%s:%s",username, password); String basicAuth = new sun.


2 Answers

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.

like image 114
priti Avatar answered Sep 18 '22 08:09

priti


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.)

like image 29
Adriano Avatar answered Sep 18 '22 08:09

Adriano