on 03-28-2018 10:12 AM - last edited on 02-04-2024 3:07 AM by postmig_api_4
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
107 | |
12 | |
11 | |
6 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.