6

我的目的是使用给定的 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 窗口

另请参阅所附图片。有人可以建议我哪里出错以及如何解决这个问题吗?

4

3 回答 3

13

我得到了我的问题的答案。我必须以编程方式明确设置“授权类型”,这样做的代码是这样的 - req.setAuthType("Preemptive");

通过给出这个,请求被发送并且我没有得到任何异常。

于 2013-06-12T13:36:56.047 回答
4

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

于 2014-04-03T17:57:05.593 回答
0

1)在我的情况下, http在属性中配置而不是https。我 用https替换了http然后它的工作。

https://localhost:8102/WebService/test

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

3) 选择 Pre-emptive auth: Authenticate pre-emptively

在此处输入图像描述

于 2018-05-16T11:31:51.117 回答