Call GP Service from C#

580
3
01-13-2022 10:23 AM
jaykapalczynski
Frequent Contributor

I have some code that I was using in a different project to pass some parameters to a GP Tool.  I am trying to reuse it but with a different C# call to a new GP Service with different parameters.

In the example below which is a JavaScript example this is how the parameters I want to use were formatted.  I need to somehow alter the JavaScript Param below into something this C# code can interpret...I dont know if this is just a formatting thing or what.

NEW PARAMETERS needed BUT from JavaScript example - variable "inputParam"

            var geometry = xxxx.Session.searchRequestTextbox.siteGeometry;
            var geometryType = (geometry.x)
                ? "point"
                : (geometry.rings)
                    ? "polygon"
                    : (geometry.paths)
                        ? "polyline"
                        : ""
                ;
            var inputParam = {
                "site": FeatureSet.fromJSON(
                    {
                        "geometryType": geometryType,
                        "spatialReference": { "wkid": 102100},
                        "features": [
                            {
                                "attributes": {
                                    "OBJECTID": 0
                                },
                                "geometry": geometry
                            }
                        ]
                    }
                ),
                "Buffer_Distance": LinearUnit.fromJSON({
                    "distance": VaFWIS.Session.searchRequestTextbox.Distance,
                    "units": "miles"
                }),
                "user": UserRoleINEED,
                "Report_Type": xxx.Session.searchRequestTextbox.reportType
            };

 

As you can see in this example below the "inputParam" was housing the parameters I was passing to this GP Tool in one long string where the python file parsed everything out and did its thing.

I need to take the parameters as seen in the JavaScript example above and make a new "inputParam" variable with the parameters from the above example.

Any thoughts?  Anyone have an idea how I can do this????

EXISTING C# CODE CALLING GP TOOL

        protected void btnGpTool_Click(object sender, EventArgs e)
        {
            string gpUrl = "https://xxxxx/arcgis/rest/services/xxx/xxxx/GPServer/Search";
            string requestUrl = gpUrl + "/execute";

            //make sure requestUrl end with execute **
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(requestUrl));
            request.KeepAlive = true;
            request.Method = "POST";
            request.AllowAutoRedirect = true;
            request.CookieContainer = new System.Net.CookieContainer();

            request.ContentType = "application/x-www-form-urlencoded";// my mistake should be form

            StringBuilder paramString = new StringBuilder();

            // THIS IS THE OLD inputParams string that needs to be replaced
            // with the example I showed above
            string inputParam = "{\"employees\":[{\"address\":\"15 Oak Ln, somewhere, Florida, 23229\",\"distance\":\"10\",\"id\":\"9a35172e071f4a33b191172a9b4b02ae\"}]}";


            paramString.Append($"request=" + inputParam);//no need ?
            paramString.Append($"&f=json");

            byte[] data = Encoding.ASCII.GetBytes(paramString.ToString());
            request.ContentLength = data.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string responseStr = getStringFromWebResponse(response);
            Response.Write(responseStr);

 

0 Kudos
3 Replies
jaykapalczynski
Frequent Contributor

This is what the GP Tool is expecting...I think this is just down to syntax on my inputParam variable?

Am I even doing this correctly...Basically the GP Tool accepts these parameters and then sends back a JSON file.  I want to capture this JSON file and simply show the text in the app for starters.

jaykapalczynski_1-1642103030439.png

 

 

 

0 Kudos
jaykapalczynski
Frequent Contributor

I am launching this from a button and when I do this is the "inputParam" to text....

This is the error I am getting after the call is attempted...

Do I have to create seperate Params for the GP Tool as seen in the the image earlier from ArcGIS Server

jaykapalczynski_3-1642105846186.png

 

        protected void btnGpTool_Click(object sender, EventArgs e)
        {
            string gpUrl = "https://xxxx/arcgis/rest/services/xxx/GP/GPServer/Search";
            string requestUrl = gpUrl + "/execute";

            //make sure requestUrl end with execute **
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(requestUrl));
            request.KeepAlive = true;
            request.Method = "POST";
            request.AllowAutoRedirect = true;
            request.CookieContainer = new System.Net.CookieContainer();

            request.ContentType = "application/x-www-form-urlencoded";// my mistake should be form

            StringBuilder paramString = new StringBuilder();

            string inputParam = @"{""site"": FeatureSet.fromJSON(
              {""geometryType"": ""esriGeometryPoint"",""spatialReference"": {""wkid"": 102100 },""features"": 
               [{""attributes"": {""OBJECTID"": 0},""geometry"": {""spatialReference"": {""latestWkid"": 3857, ""wkid"": 102100 },
               ""x"":-8911682.294077562, ""y"": 4518347.849955901 } }]}
               ),
                ""Buffer_Distance"": LinearUnit.fromJSON({""distance"": 2,
                ""units"": ""miles""}),""user"": ""username"",""Report_Type"": ""reporttype""}";

            GPToolParams.Text = inputParam;

            paramString.Append($"request=" + inputParam);
            paramString.Append($"&f=json");

            byte[] data = Encoding.ASCII.GetBytes(paramString.ToString());
            request.ContentLength = data.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            string responseStr = getStringFromWebResponse(response);
            Response.Write(responseStr);
            GPTooltext.Text = responseStr;
        }

 

0 Kudos
jaykapalczynski
Frequent Contributor

THINK I GOT IT...testing now....will report my solution when done

 

0 Kudos