9

I'm having a problem with calling a web service request in C#.

The service and request are working fine in Soap UI with the option 'Authenticate Preemptively' enabled (File, Preferences, HTTP Settings). Without this setting enabled the service returns a 'Java.Lang.NullPointerException'.

The problem I'm having is that I do not know how to enable this setting in a C# context.

I have a .NET 3.5 class library which holds a so called service reference to the specific service. This is a simple code snippet;

try
{
    CatalogService.CatalogChangeClient service = new CatalogService.CatalogChangeClient();
    service.ClientCredentials.UserName.UserName = "fancydress";
    service.ClientCredentials.UserName.Password = "47fda9cb4b51a9e";
    service.ClientCredentials.SupportInteractive = true;

    ProductUpdate[] products = new ProductUpdate[1];
    products[0] = new ProductUpdate();
    products[0].ProductCode = "00001";
    products[0].ProductDescription = "TestProduct";

    string result = service.UpdateProducts(products);
}
catch (Exception exception)
{
    Console.WriteLine(exception.Message);
}

Update after first reply.

The CatalogService.CatalogChangeClient class seems to implement the WCF abstract class

System.ServiceModel.ClientBase<TChannel>

End Update

Could anyone help me set this property?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Rob Maas
  • 475
  • 2
  • 4
  • 13
  • please show more code esp. the interface `CatalogService.CatalogChangeClient` implements – Yahia Aug 17 '11 at 08:00
  • It seems to be implementing the System.ServiceModel.ClientBase class from WCF: public partial class CatalogChangeClient : System.ServiceModel.ClientBase, Fancydress.ResourceAccess.MCSF.CatalogService.CatalogChange – Rob Maas Aug 17 '11 at 08:40
  • 1
    From what i gather this is a problem in the .NET implementation it sends the request and only when it gets a 401 from the services resends with Credentials... your Java WebService doesn't seem to send 401 - although you should first confirm that is really the case (i.e. WireShark)... IF it is the case: to circumvent this I suspect you will have to use a different client (proxy) which can be "tricked" into sending the credentials always... – Yahia Aug 17 '11 at 08:48
  • Yahia, you are spot on. Using WireShark I was able to confirm that the java webservice does not send a 401. So yes, I will need a different proxy which will send the credentials always. Thanks! – Rob Maas Aug 17 '11 at 08:57

1 Answers1

7

You could try and override the GetWebRequest method from your generated client stub. I have used this once and that solved my problem.

Look at the following URL:

http://www.eggheadcafe.com/community/wcf/18/10056093/consuming-webservices-and-http-basic-authentication.aspx

Scroll a bit down.

Here's the code from the link:

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

    if (PreAuthenticate)
    {
        NetworkCredential networkCredentials =
            Credentials.GetCredential(uri, "Basic");

        if (networkCredentials != null)
        {
            byte[] credentialBuffer = new UTF8Encoding().GetBytes(
                networkCredentials.UserName + ":" +
                networkCredentials.Password);
            request.Headers["Authorization"] =
                "Basic " + Convert.ToBase64String(credentialBuffer);
        }
        else
        {
            throw new ApplicationException("No network credentials");
        }
    }
    return request;
}
Jhon
  • 582
  • 7
  • 28
Nicklas Møller Jepsen
  • 1,248
  • 2
  • 16
  • 34