How do I add HTTP headers to JAXWS created proxy c

2019-08-11 13:51发布

问题:

I've created a set of proxy classes in NetBeans for a SOAP web service.

I'd like to add an HTTP header to the outgoing request.

Map<String, Object> requestHeaders = new HashMap<>();
requestHeaders.put("X-Header", header);
AccountManagementService service = new AccountManagementService();
AccountManagementServiceSoap soap = service.getAccountManagementServiceSoap();
GetMembershipSummaryResponse.GetMembershipSummaryResult membershipSummary = soap.getMembershipSummary("mikeb@foobar.com");

I saw a bunch of JAX-WS header examples for when you are getting the individual port from the service. Is there an easier way? Can I just add the header to some collection on service or soap object?

回答1:

after some digging I found this post: jax-ws change Content-type to Content-Type because server is hyper sensitive

which isn't really a full answer, so I am going to post the full answer here:

   AccountManagementService service = new AccountManagementService();
   AccountManagementServiceSoap soap = service.getAccountManagementServiceSoap();

   ((BindingProvider)soap).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS,
    Collections.singletonMap("X-Header",Collections.singletonList(header)));
   GetMembershipSummaryResponse.GetMembershipSummaryResult membershipSummary = soap.getMembershipSummary("mikeb@foobar.com");