Skip to Content
avatar image
Former Member

Add Business Partner via Service Layer

Hello everybody,

I try to add a new Business Partner on SAP HANA back end via Service Layer

I have tried using both .Net HttpClient and HttpWebRequest

Using HttpCliend I get an exception with Stack Trace

An error occurred while sending the request.
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()

Using HttpWebRequest I manage to connect to Service Layer but on adding new Business Partner I get a connection error

---------------------- Connection Response ----------------------
{
   "odata.metadata" : "https://192.168.1.185:50000/b1s/v1/$metadata#B1Sessions/@Element",
   "SessionId" : "fbca8422-3730-11e8-8000-08002740a590",
   "Version" : "920180",
   "SessionTimeout" : 30
}


---------------------- Add Business Partner ----------------------
The remote server returned an error: (401) Unauthorized.

Can someone please explain why I get the above results?

Best regards,

Dimitris Theotokatos

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    avatar image
    Former Member
    Apr 19 at 09:17 AM

    Hello Ralph,

    thank you for your response.

    I manage to solve my problem using RestSharp and

    var _client = new RestClient(Url);
    var _request = new RestRequest(Method);
    
    _request.AddCookie("B1SESSION", "VeryLongSessionID");
    _request.AddCookie("ROUTEID", "HeaderRouteID"]);
    _request.AddHeader("Content-type", "application/json");
    _request.AddParameter("undefined", myJsonObject, ParameterType.RequestBody);
    IRestResponse _response = _client.Execute(_request);
    

    Just leaving it here in case some else faces the same problem as I did.

    Best regards,

    Dimitris Theotokatos

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 28 at 02:17 PM

    Hi, You should pass along the B1SESSION cookie something like this

    B1SESSION dfe00e3e-31d2-11e8-8000-000c297176d4

    in all subsequent HTTP request headers. I hope this helps. Miklos

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hello Miklos,

      thank you for your reply.

      Can you please provide an example on what exactly must be the header of my request?

      Thank you,

      Dimitris Theotokatos

  • Apr 16 at 11:47 AM

    Hi,

    I did not check your code but there is a complete example of how to use Service Layer with WCF and I've done another simple code that make a couple of SL requests using pure .Net (check lib/ServiceLayer)

    Anyway, This is more a .Net coding issue than a B1 specific.

    So, better suit for StackOverflow of (how to set a cookie with .net requests) than here.

    cheers,

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 30 at 06:58 AM

    So I followed Miklos's advice and sent to Service Layer the following requests:

    1) I added B1SESSION=194903fc-3966-11e8-8000-08002740a590; ROUTEID=.node1 to my request as a Header with Name "Cookie"

    here is a sample of my request

    httpWebRequest.Headers.Add("Cookie", "B1SESSION=194903fc-3966-11e8-8000-08002740a590; ROUTEID=.node1");
    

    The responce I got was

    The remote server returned an error: (400) Bad Request.

    2) I added B1SESSION=194903fc-3966-11e8-8000-08002740a590; ROUTEID=.node1 as a Cookie using:

    if (httpWebRequest.CookieContainer == null)
    {
         httpWebRequest.CookieContainer = new CookieContainer();
    }
    Uri _target = new Uri(Url);
    
    httpWebRequest.CookieContainer.Add(new Cookie("B1SESSION", "194903fc-3966-11e8-8000-08002740a590") { Domain = _target.Host });
    httpWebRequest.CookieContainer.Add(new Cookie("ROUTEID", ".node1") { Domain = _target.Host });
    

    The response I got was

    The remote server returned an error: (400) Bad Request.

    Please note that I set my request as

    var httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";
    httpWebRequest.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
    
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
          streamWriter.Write(_json);
          streamWriter.Flush();
          streamWriter.Close();
    }
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    

    Where Url is Service Layer location and entity I need to call and _json is the json object I need to send

    Can anyone please advice how to make a request to service layer with log in session?

    Kind regards,

    Dimitris Theotokatos

    Add comment
    10|10000 characters needed characters exceeded