3

I'm writing a C# application that interacts with the SAP B1 Service Layer and I'm attempting to Login through a HTTP POST call, using JSON for the body.

If I send over the login request using POSTMAN, it logs me in fine and I receive the session cookie as expected. When I send over the JSON through my C# app, I receive a 500 error - I have absolutely no idea why. I have another method that sends over a request for a specific item which returns a 401 - Unauthorized so I know it's hitting the Service Layer.

Below is the code in the method so far.

var request = (HttpWebRequest)WebRequest.Create("https://172.16.101.38:50000/b1s/v1/Login");
            request.ContentType = "application/json";
            request.Method = "POST";
            request.ServerCertificateValidationCallback = AcceptAllCertifications;

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                string json = new JavaScriptSerializer().Serialize(new
                {
                    UserName = "user",
                    Password = "pass",
                    CompanyDB = "db"
                });

                streamWriter.Write(json);
            }

            var response = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }

And the "AcceptAllCertifications" method is as follows:

public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, 
        System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }

If I don't include this method, I get a "Could not establish a trust relationship for the SSL/TLS secure channel..." error. In POSTMAN, I have to ensure "SSL Certificate verification" is OFF so that I can access the Service Layer.

I know the JSON produced is valid as I've tested it in POSTMAN.

Anyone run into this before and managed to figure out how to rectify this?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • 1
    _"I receive a 500 error - I have absolutely no idea why"_ - from whom, from your web application, or from the SAP web service? Either way, obtain the actual error, a "500" is just as useless as "an error". It _probably_ has to do with TLS 1.2, but that's just guessing. – CodeCaster Nov 01 '18 at 09:32
  • @CodeCaster the error comes from SAP's web service. I'd like to view the error logs to obtain the actual error but I don't have access to them unfortunately. – PoweredByDoritos Nov 01 '18 at 09:35
  • you'd have to ask the people who maintain the SAP service to help you, then. We haven't got any usable information in order to do so. – ADyson Nov 01 '18 at 10:01
  • @Adyson Fortunately I'm currently in the process of getting some logs now, so when I figure it out I'll be able to (hopefully) provide an answer. The question was more in hope that someone else had something similar happen that they managed to rectify in case getting hold of said logs took longer than expected. – PoweredByDoritos Nov 01 '18 at 10:04
  • 1
    I get that, but unfortunately we don't know what "something similar" is...the error could be a hundred things – ADyson Nov 01 '18 at 10:21
  • @ADyson Yeah I do appreciate it's very vague, just didn't know if someone else had something similar with SAP and had an answer :-) as it turns out, the SAP logs are useless, just says 500 with no further information. Will continue banging my head against this and when I eventually figure it out I'll post the answer. – PoweredByDoritos Nov 01 '18 at 10:29
  • someone found the solution – Hackerman Jun 06 '22 at 14:09

2 Answers2

3

Add below line in your request.

request.ServicePoint.Expect100Continue = false;
Syscall
  • 19,327
  • 10
  • 37
  • 52
  • Hi, welcome to SO. Thanks for your answer. Answer longevity can be improved by explaining how this solves the posted issue (especially since this question is over two years old and the asker likely isn't concerned about this anymore). – Connor Low Mar 05 '21 at 17:14
  • 1
    Hi there, thanks for this answer! @ConnorLow is quite right, I am no longer concerned with this anymore ; however, if this answer is better than the answer I posted myself, that's great! Please do elaborate some more as to why this line of code works, and how it does so. – PoweredByDoritos May 25 '21 at 15:01
  • It works for me. Most HTTP methods in C# append `Expect: 100-continue` header by default, but SAP Service Layer `/Login` returns 500 error when the header is specified. [@muhammad-ali](https://stackoverflow.com/users/15336595/muhammad-ali)'s code prevents sending the header. My SAP Service Layer is 10.00.120. – nobyk Aug 23 '22 at 13:39
  • just like @nobyk said it did not return any useful information making this problem very difficult to diagnose. I had to contact SAP support for this solution. Upvoted this as we are likely not the only one who will encounter this – Cees Feb 02 '23 at 05:45
0

Here is a link to the solution I've had to implement:

SAP Consume OData Services

So instead of trying to access everything through JSON, I've had to take some code from here which creates a Service Layer instance and logs in through a method implemented in there.

Very fiddly to get working at first, but once it is working it's pretty solid.