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?