In-Depth Analysis: Adding Custom HTTP Headers to C# Web Service Clients for Consuming Axis 1.4 Web Services

Dec 02, 2025 · Programming · 13 views · 7.8

Keywords: C# | Web Service | HTTP Headers | Axis 1.4 | .NET 2.0

Abstract: This article explores methods for adding custom HTTP headers (e.g., Authorization: Basic Base64EncodedToken) to C# clients consuming Java Axis 1.4 web services. Focusing on the solution of overriding the GetWebRequest method, which modifies generated protocol code to inject headers during web request creation. Alternative approaches using OperationContextScope and custom message inspectors are discussed as supplements, analyzing their applicability and trade-offs. Through code examples and theoretical insights, it provides comprehensive guidance for authentication in .NET 2.0 environments.

Introduction

In cross-platform web service integration, C# clients consuming web services built with Java Axis 1.4 often need to handle custom HTTP headers, such as Authorization: Basic Base64EncodedToken. Since Axis services require this header for authentication, and standard WSDL-generated references or WSE 3.0 lack direct support in .NET 2.0 environments, developers must seek alternative solutions. Based on Q&A data, this article delves into core methods and provides supplementary references to address this technical challenge.

Core Solution: Overriding the GetWebRequest Method

The best answer (score 10.0) proposes adding custom HTTP headers by overriding the GetWebRequest method. This approach is suitable for modifying auto-generated protocol code, allowing header injection during web request creation. Implementation steps include:

  1. In the generated web service client class, locate or add an override of the GetWebRequest method, which is defined in the base class to create a System.Net.WebRequest object.
  2. In the override, call the base method to obtain the request object, then use Headers.Add to add custom headers. For example, to add an Authorization header: request.Headers.Add("Authorization", "Basic " + base64Token);, where base64Token is a Base64-encoded authentication token.
  3. Return the modified request object. Ensure removal of the DebuggerStepThroughAttribute (if present) to enable stepping through this code during debugging.

Code example:

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
    System.Net.WebRequest request = base.GetWebRequest(uri);
    request.Headers.Add("Authorization", "Basic " + "Base64EncodedToken");
    return request;
}

This method's advantage lies in its direct modification of the request pipeline, offering simplicity and efficiency for all System.Net.WebRequest-based web service calls. However, note that modifying generated code may affect future updates; backing up or using partial class extensions is recommended.

Supplementary Approach One: Using OperationContextScope

Answer two (score 5.8) addresses WCF environments by using OperationContextScope to wrap service calls. Although the question specifies .NET 2.0 and non-WCF, this method remains relevant in similar contexts. It sets the Authorization header via HttpRequestMessageProperty, suitable for scenarios requiring dynamic header injection.

Key implementation points:

Code snippet:

using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
    var httpRequestProperty = new HttpRequestMessageProperty();
    httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
    // Call service method
}

Supplementary Approach Two: Custom Message Inspector

Answer three (score 3.5) introduces injecting HTTP headers via a custom message inspector (IClientMessageInspector) and endpoint behavior (IEndpointBehavior). This solution is ideal for reusable, non-intrusive header management, especially in large-scale projects.

Critical steps:

  1. Implement the IClientMessageInspector interface, adding custom headers to HttpRequestMessageProperty in the BeforeSendRequest method.
  2. Implement the IEndpointBehavior interface, adding the inspector to the client runtime in the ApplyClientBehavior method.
  3. When instantiating the client, add this behavior: client.Endpoint.EndpointBehaviors.Add(new CustomAuthenticationBehaviour("token"));.

Code example:

public class CustomMessageInspector : IClientMessageInspector
{
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        var reqMsgProperty = new HttpRequestMessageProperty();
        reqMsgProperty.Headers.Add("Auth-Token", "value");
        request.Properties[HttpRequestMessageProperty.Name] = reqMsgProperty;
        return null;
    }
    public void AfterReceiveReply(ref Message reply, object correlationState) { }
}

This method offers better encapsulation and testability but is more complex to implement, suited for advanced customization needs.

Analysis and Comparison

Based on the above solutions, key insights are distilled:

In practice, overriding GetWebRequest is recommended as the primary solution due to its high score and directness in the given context. For complex projects, consider custom inspectors to enhance code quality.

Conclusion

Adding custom HTTP headers to C# web service clients for consuming Axis 1.4 services centers on understanding extension points in the request pipeline. Overriding the GetWebRequest method provides a simple and effective solution for most .NET 2.0 scenarios. Supplementary approaches like OperationContextScope and custom message inspectors offer alternative paths for specific needs. Developers should choose based on project environment, maintenance requirements, and performance considerations. Future upgrades to higher .NET versions may explore new features in WCF or ASP.NET Core to simplify such integration tasks.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.