C# – consume SAP Webservice within .net application – authentication error 401

cnetsapweb services

I have been trying to consume a simple SAP web service within a .net c# console application (.NET Framework 4.5) for hours.

The sap web service was created and activated by myself reffering to this article -> http://scn.sap.com/docs/DOC-38805 . It just passes "hello world" when calling the service.

The web service uses basic http auth with username/password. If I call the service with SoapUI and use pre-emptively basic authentication I receive the desired response. The first request package sent from the client to the server includes an authorization header and the service works properly.

Referring to this document -> http://scn.sap.com/docs/DOC-38805 I created a .Net console application, added the web reference and inserted following code:

    sapref1.Z_HELLO_WORLD_TEST2 client = new sapref1.Z_HELLO_WORLD_TEST2();           
sapref1.Z_ERSMA52_TESTSERVICE client_input = new sapref1.Z_ERSMA52_TESTSERVICE();
sapref1.Z_ERSMA52_TESTSERVICEResponse client_response = new sapref1.Z_ERSMA52_TESTSERVICEResponse();

client.PreAuthenticate = true;
client.Credentials = new NetworkCredential("user", "password");
client_response = client.Z_ERSMA52_TESTSERVICE(client_input);

If I run the application for the first time, I receive a response from the web service ("hello world") but if I run the application again I always receive an unauthorized exception. I analysed the network traffic with wireshark. The first request sent from the client does not include an authorization header and the server responds with an error code 401 which leads to the mentioned exception.

I read several articels about this problem but I didn't find any solution that works for me. Is there any way to consume the SAP web service with a proxy class which is generated through "add web reference"-function in VS 2013 ?
Thanks in advance!

Best Answer

I'm not a C# developer, but this answer seems to suggest a workable solution, allowing you to pre-emptively send the authentication details.

Related Topic