Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom Http Header for C# Web Service Client consuming Axis 1.4 Web service

I'm trying to write a web service client in c# which the webservice is Java Axis 1.4. Axis service requires the Authorization: Basic Base64EncodedToken header value in the HTTP Headers. I can't find a way to set this header in standart ways of consuming web services in visual studio.net, like normal WSDL generated refernce nor with WSE3.0

I can't use WCF as the project is developed using .net 2.0.

Is there any way to do this ?

like image 745
UmutKa Avatar asked May 22 '09 13:05

UmutKa


People also ask

Can you add custom HTTP headers?

Another example of using a custom HTTP header would be to implement the X-Pull header. You can use this custom header for a variety of purposes including rate limiting bandwidth on your origin server, restricting CDN traffic, creating custom logic on your origin server, etc.

How do I add a header to my HTTP request?

To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers. For example: oRequest = RequestBuilder:Build('GET', oURI) :AddHeader('MyCustomHeaderName','MyCustomHeaderValue') :AddHeader('MySecondHeader','MySecondHeaderValue') :Request.

How do I create a custom request header?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.


2 Answers

It seems the original author has found their solution, but for anyone else who gets here looking to add actual custom headers, if you have access to mod the generated Protocol code you can override GetWebRequest:

protected override System.Net.WebRequest GetWebRequest(Uri uri) {   System.Net.WebRequest request = base.GetWebRequest(uri);   request.Headers.Add("myheader", "myheader_value");   return request; } 

Make sure you remove the DebuggerStepThroughAttribute attribute if you want to step into it.

like image 175
user334291 Avatar answered Oct 02 '22 16:10

user334291


Are we talking WCF here? I had issues where the service calls were not adding the http authorization headers, wrapping any calls into this statement fixed my issue.

  using (OperationContextScope scope = new OperationContextScope(RefundClient.InnerChannel))   {             var httpRequestProperty = new HttpRequestMessageProperty();             httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " +             Convert.ToBase64String(Encoding.ASCII.GetBytes(RefundClient.ClientCredentials.UserName.UserName + ":" +             RefundClient.ClientCredentials.UserName.Password));             OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;              PaymentResponse = RefundClient.Payment(PaymentRequest);    } 

This was running SOAP calls to IBM ESB via .NET with basic auth over http or https.

I hope this helps someone out because I had massive issues finding a solution online.

like image 30
Simon RC Avatar answered Oct 02 '22 15:10

Simon RC