cancel
Showing results for 
Search instead for 
Did you mean: 

SAP Business One Service Layer:The remote server returned an error: (502) Bad Gateway.

Former Member
0 Kudos

Hi,

I am trying to login to Service layer in c# url using format

POST https://:/b1s/v1/Login

{"CompanyDB": "US506", "UserName": "manager", "Password": "1234"}

I am constantly getting the following error "The remote server returned an error: (502) Bad Gateway."

Below is the code I am using:

        private static bool RemoteSSLTLSCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors ssl)

        {

            //accept

            return true;

        }

static void Interact(string url, string method, string body = "")

        {

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            httpWebRequest.KeepAlive = true;

            httpWebRequest.Accept = "application/json;odata=minimalmetadata";

            httpWebRequest.AllowAutoRedirect = true;

            httpWebRequest.ContentType = "application/json;odata=minimalmetadata;charset=utf8";

            httpWebRequest.Headers.Add("Prefer", "odata.maxpagesize=10");

            httpWebRequest.Method = method.ToUpper();

            ServicePointManager.ServerCertificateValidationCallback += RemoteSSLTLSCertificateValidate;

            if (!string.IsNullOrEmpty(body))

            {

                using (var stream = httpWebRequest.GetRequestStream())

                {

                    StreamWriter writer = new StreamWriter(stream);

                    writer.Write(body);

                    writer.Dispose();

                }

            }

            try

            {

                using (var response = httpWebRequest.GetResponse())

                {

                    var reader = new StreamReader(response.GetResponseStream());

                    var data = reader.ReadToEnd();

                }

            }

            catch (Exception ex)

            {

            }

        }

Found that the web exception in details is

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>502 Proxy Error</title>

</head><body>

<h1>Proxy Error</h1>

<p>The proxy server received an invalid

response from an upstream server.<br />

The proxy server could not handle the request <em><a href="https://answers.sap.com/b1s/v1/Login">POST /b1s/v1/Login</a></em>.<p>

Reason: <strong>Error reading from remote server</strong></p></p>

</body></html>

Please help. Am unable to get my head around what I am doing wrong.

Thanks and Regards,

Saurav

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Have the same problem here! any idea?

my code:

private void LoginRest()

{

    string result = "";

    string json = "{\"CompanyDB\": \"SBODEMOCH_TEST\", \"UserName\": \"manager\", \"Password\": \"manage\"}";

    using (var client = new WebClient())

    {

         client.Headers[HttpRequestHeader.ContentType] = "application/json";

         ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

        

             

          result = client.UploadString("https://myservername:50000/b1s/v1/Login", "POST", json);

    }

          Console.WriteLine(result);

}