Skip to Content
0
Former Member
Aug 13, 2016 at 10:38 AM

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

967 Views

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="/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