2

I'm creating a .NET application that uses a web service. I need set the connection http header to "closed" in the request to that web service. I've been Googling this for a day but have not been able to get anything to work.

My best effort is the code below, which attempts to override the GetWebRequest method to add the header. This appears to fail - I place a breakpoint in it, and when I run my application, the breakpoint is never hit and the connection header does not appear to be set (I'm evaluating this not by viewing the http header but by the behavior of the system handling the web request).

Some information: when I added the web reference, using Visual Studio, I right-clicked on the project in the solution explorer, chose "Add Service Reference", "Advanced", then "Add Web Reference".

namespace System.Net
{
    public class MyHttpProtocol : SoapHttpClientProtocol
    {
        protected override WebRequest GetWebRequest(Uri uri)
        {
            HttpWebRequest webRequest = (HttpWebRequest)base.GetWebRequest(uri);
            webRequest.Headers.Add("connection", "closed");
            return webRequest;
        }
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Louis Savalli
  • 31
  • 2
  • 7
  • Post the code that is supposed to use this please. Also are you trying to acces a soap service? If so does it have a WSDL? – CSharpie Jun 21 '13 at 17:25
  • @CSharpie, apparently that was not getting called anywhere. I'm relatively new to overriding methods in C# and I'm not sure I was taking the right approach at all. I thought if I overrode GetWebRequest, .net would call it during the outgoing web service request. That did not appear to be happening. – Louis Savalli Jun 24 '13 at 15:20
  • Final Solution: I can't say this question is answered because I still can't modify http headers using the web reference, but ultimately I switched to using WCF and creating a custom binding to set the KeepAliveEnabled property to false. This had the desired effect. I found this solution here: http://stackoverflow.com/questions/6535074/wcf-wshttpbinding-with-http-keepalive?rq=1 – Louis Savalli Jun 24 '13 at 15:22

1 Answers1

-1

You need to use an IClientMessageInspector to alter the request as it's being constructed. This question is very similar and should give you the answer.

Many things in WCF involve creating behaviours and specifying them in the web config to override certain aspects of the process, which can be a bit fiddly but is very powerful. You can add your request in there.

Edit to address your comment, does your code look like this?

public object BeforeSendRequest(
    ref System.ServiceModel.Channels.Message request,
    System.ServiceModel.IClientChannel channel)
{
    var httpRequestMessage = new HttpRequestMessageProperty();
    httpRequestMessage.Headers.Add("connection", "closed");
    request.Properties.Add(
        HttpRequestMessageProperty.Name, httpRequestMessage);
    return null;
}
Community
  • 1
  • 1
NibblyPig
  • 51,118
  • 72
  • 200
  • 356
  • Thanks, I will try that and follow-up here. – Louis Savalli Jun 21 '13 at 15:19
  • So because I'm not using WCF, the IClientMessageInspector approach will not work? – Louis Savalli Jun 21 '13 at 16:26
  • That's correct. However, you should be just using "Add Service Reference" without the "Advanced" part. Then you could use `IClientMessageInspector` – John Saunders Jun 21 '13 at 16:56
  • I'm currently working on this; I've switched to WCF and have the IClientMessageInspector in place and working. Problem is that Visual Studio keeps throwing an exception because I'm trying to set the Connection http header to "closed" - "Keep-Alive and Close may not be set using this property". If I can get it to work, then I'll consider the switch to WCF as the answer and mark this as solved. – Louis Savalli Jun 22 '13 at 16:33
  • Added a bit to my answer, is that what you're doing? – NibblyPig Jun 24 '13 at 12:41
  • Yes; though I actually have a condition to check if that header already exists before I add it. But the code you wrote is almost exactly what I have. – Louis Savalli Jun 24 '13 at 13:13