How to Add Http Header to Soap Client

Add header to SOAP service client

To work with the raw request you either need to create a request interceptor or just set the headers on the requested context on the service port. So in your case, it should be as simple as

@RestController
@RequestMapping("/codes")
public class CodigosRestController {
@GetMapping
public String obtainCode(){
CodesService_Service service = new CodesService_Service();
CodesService port = service.getCodesServicePort();
// generate the headers
Map<String, List<String>> requestHeaders = new HashMap<>();
requestHeaders.put("apiKey",Arrays.asList("TokeApi yourtokenhere"));
BindingProvider bindingProvider = (BindingProvider) port;
// set the headers on the request context
bindingProvider.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders);

String code = port.methodTwo().toString();
return code;
}

}

Adding Http Header in an existing WCF SOAP Service is not working

SOAP Header

To add a SOAP header, use the following code client-side:

using (OperationContextScope scope = new OperationContextScope((IContextChannel)channel))
{
MessageHeader<string> header = new MessageHeader<string>("MyHttpHeaderValue");
var untyped = header.GetUntypedHeader("User-Auth", ns);
OperationContext.Current.OutgoingMessageHeaders.Add(untyped);

// now make the WCF call within this using block
}

And then, server-side, grab it using:

MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
string identity = headers.GetHeader<string>("User-Auth", ns);

NB. ns is The namespace URI of the header XML element.

HTTP Header

To add an Http header:

// Add a HTTP Header to an outgoing request 
HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
requestMessage.Headers["MyHttpHeader"] = "MyHttpHeaderValue";
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;

And to grab it server-side

IncomingWebRequestContext request = WebOperationContext.Current.IncomingRequest; 
WebHeaderCollection headers = request.Headers;
Console.WriteLine(request.Method + " " + request.UriTemplateMatch.RequestUri.AbsolutePath);

foreach (string headerName in headers.AllKeys)
{
Console.WriteLine(headerName + ": " + headers[headerName]);
}

Adding SOAP Header to request

Try to use this one

private static void Main()
{
using (var client = new ServiceClient())
using (var scope = new OperationContextScope(client.InnerChannel))
{
MessageHeader usernameTokenHeader = MessageHeader.CreateHeader("UsernameToken",
"http://test.com/webservices", "username");
OperationContext.Current.OutgoingMessageHeaders.Add(usernameTokenHeader);

MessageHeader passwordTextHeader = MessageHeader.CreateHeader("PasswordText",
"http://test.com/webservices", "password");
OperationContext.Current.OutgoingMessageHeaders.Add(passwordTextHeader);

MessageHeader sessionTypeHeader = MessageHeader.CreateHeader("SessionType",
"http://test.com/webservices", "None");
OperationContext.Current.OutgoingMessageHeaders.Add(sessionTypeHeader);

string result = client.GetData(1);
Console.WriteLine(result);
}
Console.ReadKey();
}

The Service Trace viewer shows following

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<UsernameToken xmlns="http://test.com/webservices">username</UsernameToken>
<PasswordText xmlns="http://test.com/webservices">password</PasswordText>
<SessionType xmlns="http://test.com/webservices">None</SessionType>
<To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:13332/Service1.svc</To>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/GetData</Action>
</s:Header>
</s:Envelope>

Take a look OperationContextScope for more info

how to add a http header to a soaprequest in java

I found a solution for the problem two months ago. You are not able to set a customized header with Axis2. So I fell back to the older Axisversion, where you can do it. Setting the Http-header by yourself is no good practice and mostly unneccesary. On top it is not a part of the SOAP-specification. That is the reason why you cannot do it with Axis2.



Related Topics



Leave a reply



Submit