尝试通过 Java 设置用户名和密码向 SOAP WsdlRequest 提交请求时出现 ClientProtocolException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17065367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ClientProtocolException when trying to submit request to a SOAP WsdlRequest by setting username and password via Java
提问by priti
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.
我的目的是使用给定的 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);
}
}
}
Also refer to the image attached. Can somebody suggest me where am I going wrong and how to resolve this problem?
另请参阅所附图片。有人可以建议我哪里出错了以及如何解决这个问题?
回答by priti
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");
我得到了我的问题的答案。我必须以编程方式显式设置“授权类型”,而这样做的代码是这样的 -
req.setAuthType("Preemptive");
By giving this, the request is being sent and I am not getting any exceptions.
通过给出这个,请求正在发送,我没有收到任何异常。
回答by Adriano
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.)
在我寻找修复此相同错误的过程中,我发现解决方案是启用“先发制人身份验证”选项(进入首选项-> http 设置并选中“先发制人身份验证”框。)
回答by Narayan Yerrabachu
1) In my case httpconfigured in properties instead https. I replaced httpwith httpsthen its working.
1)在我的情况下,在属性中配置了http而不是https。我用https替换了 http,然后它就可以工作了。
https://localhost:8102/WebService/test
2) Check Authorization username and Password
2) 检查授权用户名和密码
3) Select Pre-emptive auth: Authenticate pre-emptively
3) 选择 Pre-emptive auth: Authenticate pre-emptively