尝试通过Java设置用户名和密码向SOAP WsdlRequest提交请求时的ClientProtocolException

时间:2013-06-12 12:21:03

标签: java soapui

我的目的是使用给定的wsdl URL创建一个Soap UI项目,保存它并通过Java方法发送所有请求。当我尝试通过设置操作的用户名和密码来提交请求时,我收到org.apache.http.client.ClientProtocolException..... Caused by: org.apache.http.ProtocolException: Content-Length header already present错误。找到我的Java方法来发送请求。

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

另请参阅附图。有人可以告诉我哪里出错了以及如何解决这个问题?

3 个答案:

答案 0 :(得分:10)

我得到了我的问题的答案。 我必须设置授权类型&#39;明确地和编程地,这样做的代码是这样的 - req.setAuthType("Preemptive");

通过这样做,请求正在发送,我没有任何例外。

答案 1 :(得分:2)

在我的搜索中修复了同样的错误,我发现解决方案是启用“认证抢先”选项(进入偏好设置&gt; http设置并选中'认证抢先'框。)

答案 2 :(得分:0)

1)在我的案例中, http 在属性中配置 https 。我换了     http https ,然后正常工作。

https://localhost:8102/WebService/test

2)检查授权用户名和密码

3)选择Pre-emptive auth:先发制人认证

enter image description here