AnsweredAssumed Answered

PI WebAPI Batch request from C# - System.Net.WebException

Question asked by Arco on Feb 9, 2017
Latest reply on Mar 29, 2017 by Arco

Hi all,


I am getting to know the PI WebAPI and am trying to do some Batch requests as explained in the Batch webinar ( PI Developers Club Webinar Series: PI Web API - Batch ).

I installed Postman as done in the webinar and was able to do Batch requests, so the PI WebAPI is able to do it (version 2016 R2). So far so good.


Now I would like to do a batch request from C# code. I am able to do Post requests via code. I successfully updated a value on a PI Point, so that works.

But I am stuck at sending a Batch request somehow and I need some help.

At the point where I am sending my request (Stream dataStream = request.GetRequestStream();) I get the following error:


An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code

Additional information: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.


As said, it works for other POST requests


I snitched some code from the OSISoft git repository and adjusted this for my needs.

Here is my code:


        public void BatchRequest()
            string controller = @"https://server/piwebapi/batch";
            dynamic globalBatch = new JObject();
            globalBatch["1"] = new JObject();
            globalBatch["1"].Method = "GET";
            globalBatch["1"].Resource = @"https:/server/piwebapi/streams/P0AAAAAAAAAAAAAAAAAAFz8AEQAAAAUzA4OEE4MjAwXE5JQV9DX0ZQ/value";

            globalBatch["2"] = new JObject();
            globalBatch["2"].Method = "GET";
            globalBatch["2"].Resource = "https://server/piwebapi/streams/P0AAAAAAAAAAAAAAAAAAFz8AEQAAAAUzA4OEE4MjAwXE5JQV9DX0ZQ/value";

            string data = JsonConvert.SerializeObject(globalBatch);

            int statusCode;
            PIWebAPIWrapper.Infrastructure.PIWebAPIConnector.MakePostRequest(controller, data, out statusCode);

      //This function is responsable for the HTTP POST request
        public static void MakePostRequest(string url, string postData, out int statusCode)
            WebRequest request = WebRequest.Create(url);

            //Kerberos Authentication Only
            request.UseDefaultCredentials = true;

            ((HttpWebRequest)request).UserAgent = ".NET Framework Example Client";

            //The method is "GET" by default.
            request.Method = "POST";

            //If the content-type is not specified as application/json, your post request won't work.
            request.ContentType = "application/json";

            //The content length should be present on the request header.
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = byteArray.Length;
            // Here I get the exception
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);

            //Get the response to make sure the value was sent.
            WebResponse response = request.GetResponse();

            //The StatusCode property is available only if the WebRequest object is converted to HttpWebRequest
            //The StatusCode will confirm that the operation was executed successfully.
            statusCode = Convert.ToInt32(((System.Net.HttpWebResponse)(response)).StatusCode);

When I take the postData string from code and use this in Postman, I get the result I am expecting, so the postData seems legit.

I tried several other batch requests but did not succeed.


Does anyone have an idea what I am doing wrong?