简体   繁体   English

在java Web服务客户端中设置头信息

[英]setting header info in java web service client

I hope someone can help me. 我希望有一个人可以帮助我。 I have been asked to create a client to call a web service, but the developers of the web service are less than helpful and have provided scant information. 我被要求创建一个客户端来调用Web服务,但Web服务的开发人员不太乐于助人并提供了很少的信息。

I have created the service client in NetBeans and I have added a handler (from examples I have seen) in order to set username and password for Basic Authentication. 我在NetBeans中创建了服务客户端,并添加了一个处理程序(来自我见过的示例),以便为基本身份验证设置用户名和密码。 However, when I run the project I am told that authentication failed, so either I am sending the wrong information or I need to do some more. 但是,当我运行项目时,我被告知身份验证失败,所以要么我发送了错误的信息,要么我需要做更多的事情。 What I really don't understand is this: the developers of the web service say that the header should look like the following, so could anyone tell me how I achieve that, or is it all done for me? 我真正不明白的是:Web服务的开发人员说标题应该如下所示,所以有人能告诉我我是如何实现的,还是这一切都是为我完成的?

POST https:// … POST https:// ...

Accept-Encoding: gzip,deflate Accept-Encoding:gzip,deflate

Content-Type: text/xml;charset=UTF-8 Content-Type:text / xml; charset = UTF-8

SOAPAction: ”” SOAPAction:“”

Authorization: Basic MjAwKKAwNjAwMTpQcjBlZSRza!:uYWw= 授权:基本MjAwKKAwNjAwMTpQcjBlZSRza!:uYWw =

Content-Length: 848 内容长度:848

Host: … 主持人:......

Connection: Keep-Alive 连接:保持活力

User-Agent: Apache-HttpClient/4.1.1 User-Agent:Apache-HttpClient / 4.1.1

thanks in advance, 提前致谢,

You can use BindingProvider to set username password for basic authentication. 您可以使用BindingProvider为基本身份验证设置用户名密码。 For eg. 例如。

    Service service = Service.create(url, qname);
    HelloWorld hello = service.getPort(HelloWorld.class);

    /******************* Set UserName and Password ******************************/
    Map<String, Object> req_context = ((BindingProvider)hello).getRequestContext();
    req_context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);

    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    headers.put("Username", Collections.singletonList("yogesh"));
    headers.put("Password", Collections.singletonList("patil"));
    req_context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
    /**********************************************************************/

Also check this thread for more help. 另请查看此主题以获取更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM