0

I am updating an older tool that we have been using for over 10 years that is similar to SoapUI, but C# based. The original code is using this function to connect to a WCF web service and download the WSDL information:

        DiscoveryClientProtocol discoveryClientProtocol = new DiscoveryClientProtocol();
        discoveryClientProtocol.AllowAutoRedirect = true;
        discoveryClientProtocol.UseDefaultCredentials = true;
        discoveryClientProtocol.DiscoverAny(serviceDescriptionURL.URL);
        discoveryClientProtocol.ResolveAll();

We recently migrated our WCF web services to the cloud and I need to be able to send an authentication header with all requests to the URL. I know that DiscoveryClientProtocol is inherited from HttpWebClientProtocol and WebClientProtocol, but so far I haven't figured out how to modify the code in order to include a header.

I have tried playing with the CookieContainer() collection to see if adding a System.Net.Cookie would translate to an authentication header, but so far all attempts to add the cookie have failed with various error messages. I don't know if this is the correct path to go down.

I also have captured my traffic using Fiddler and can see that the request is running into the expected authentication error. If I rerun the request in Fiddler with the proper header, it works fine. I just need to understand if there is a way to add that header programmatically.

Is this possible? If so, what do I need to do to add a header or authentication?

Thanks in advance!

1 Answers1

0

Thanks to another posted (who also happened to answer their own question), I was able to figure out what I needed to do. Here is the link to their question/answer for reference:

adding http headers in call to SoapHttpClient service

The solution is to extend the DiscoverClientProtocol class and then override the underlying GetWebRequest method. Here's an example of what I did:

    protected override System.Net.WebRequest GetWebRequest(Uri uri)
    {
        var request = base.GetWebRequest(uri);

        // Add custom headers
        foreach (Header header in HeaderList)
        {
            request.Headers.Add(header.HeaderKey, header.HeaderValue);
        }

        return request;
    }

Hope this helps anyone that may happen to have this same situation arise!