Preauthenticate web service request in C #

I am having a problem calling a web service request in C #.

The service and request work fine in the Soap user interface with the "Authenticate Preemptively" option enabled ("File", "Settings", "HTTP Settings"). Without this parameter, the service returns the message "Java.Lang.NullPointerException".

The problem I am facing is that I do not know how to enable this option in a C # context.

I have a .NET 3.5 class library that contains a so-called service link to a specific service. This is a simple piece of code,

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 the first answer.

The CatalogService.CatalogChangeClient class apparently implements the WCF abstract class

 System.ServiceModel.ClientBase<TChannel> 

Final update

Can someone help me set this property?

+7
source share
1 answer

You can try and override the GetWebRequest method from the created client stub.

I used it once and it solved my problem. Take a look at the following: http://www.eggheadcafe.com/community/wcf/18/10056093/consuming-webservices-and-http-basic-authentication.aspx Scroll down a bit.

Here is 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; } 
+5
source

All Articles