cancel
Showing results for 
Search instead for 
Did you mean: 

Post data using WebClient in C# and server replay Bad Gateway

0 Kudos

Hello.

I need to use ServiceLayer on my server Hana per obtain a Login.

I write this c# code, but when I execute this code, my server Hana replay me with the message Bad Gateway (502)

ServicePointManager.ServerCertificateValidationCallback=(sender, certificate, chain, errors)=>true;string result ="";string json ="{\"CompanyDB\" : \"SBODEMOIT\", \"UserName\": \"manager\", \"Password\": \"manager\"}";
        using (var client =new WebClient()){
            client.Headers[HttpRequestHeader.ContentType]="application/json";
            client.Encoding=Encoding.UTF8;
            result = client.UploadString("https://b1dbhana:50000/Login","POST", json;}

And so I try to use Arc (Advanced rest Client Application) for testing Login on my server, it response correctly with a SessionId.

Where is the error in my code?

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

This is the correct example for login

ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate cert,
                                                                    X509Chain chain,
                                                                    SslPolicyErrors sslPolicyErrors)
                                                                    { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://<myServer>:50000/b1s/v1/Login");
httpWebRequest.Accept = "application/json";
httpWebRequest.KeepAlive = true;
httpWebRequest.ServicePoint.Expect100Continue = false;
httpWebRequest.AllowAutoRedirect = true;
httpWebRequest.ContentType = " application/json;odata=minimalmetadata;charset=utf-8";
httpWebRequest.Timeout = 10000000;
httpWebRequest.Method = "POST";
httpWebRequest.UserAgent = "Microsoft ADO.NET Data Services";
 
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = new JavaScriptSerializer().Serialize(new
    {
        CompanyDB = "<myCompany>",
        UserName = "<myUsername>",
        Password = "<myPassword>"
    });
    streamWriter.Write(json);
}
 
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    Console.WriteLine(streamReader.ReadToEnd());
}

The using command is necessary per disposing streamWriter object.

Alternatively at the using block this is the possibility

var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());
string json = new JavaScriptSerializer().Serialize(new
{
    CompanyDB = "SBODEMOIT",
    UserName = "manager",
    Password = "manager"
});
streamWriter.Write(json);
streamWriter.Dispose();

Answers (0)