How to Send Parameter to SOE through HTTP based services?

2808
2
09-20-2019 10:01 AM
ShaikhRizuan
New Contributor III

Hi,

I am having "Location" parameter where 'x' & 'y' value has to pass in SOE to process the request.Below is the C# code to access the SOE request.

// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("https://myServer.com:6443/arcgis/rest/services/ITDService/MapServer/exts/DistanceSOE/SpatialQuery");
// Set the Method property of the request to POST.
request.Method = "POST";

// Create POST data and convert it to a byte array.
string postData = "{\"Location\": {\"x\": 84.44902820440936,\"y\": 21.300696836245145}}";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;

// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();

// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);

// Get the stream containing content returned by the server.
// The using block ensures the stream is automatically closed.
using (dataStream = response.GetResponseStream())
{
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
}

It is going to SOE but could not able to read the "Location" Parameter and its values. PFB image for the same.

Can anyone let know, how parameters need to be send?

0 Kudos
2 Replies
nicogis
MVP Frequent Contributor
private RestResource CreateRestSchema()
        {
            RestResource rootRes = new RestResource(soe_name, false, RootResHandler);

            RestOperation sampleOper = new RestOperation("sampleOperation",
                                                      new string[] { "Location" },
                                                      new string[] { "json" },
                                                      SampleOperHandler);
            rootRes.operations.Add(sampleOper);

        
            return rootRes;
        }

JsonObject parm1Value;
bool found = operationInput.TryGetJsonObject("Location", out parm1Value);
if (!found)
    throw new ArgumentNullException("Location");

IPoint location = Conversion.ToGeometry(parm1Value,
        esriGeometryType.esriGeometryPoint) as IPoint;

when you call soe you an write similar code:

void Main()
{


	Location location = new Location() { x = 100.3d, y = 500.9d };
	JavaScriptSerializer js = new JavaScriptSerializer();
	string jsonData = js.Serialize(location);
	
	Dictionary<string, string> dictionary = new Dictionary<string, string>();
	dictionary.Add("f", "json");
	dictionary.Add("Location", jsonData);
	
	Request.HttpPost("http://...../MapServer/exts/TestSOE/sampleOperation", dictionary);
       
	
	

}

internal class Location
{
	public double x;
	public double y;
}

/// <summary>
/// class request rest
/// </summary>
internal static class Request
{
	/// <summary>
	/// method http get
	/// </summary>
	/// <typeparam name="T">object T</typeparam>
	/// <param name="url">url for get</param>
	/// <returns>return object T</returns>
	public static T HttpGet<T>(string url)
	{
		string result = Request.HttpGet(url);

		var serializer = new JavaScriptSerializer();
		var deserializedResult = serializer.Deserialize<T>(result);
		return deserializedResult;
	}

	/// <summary>
	/// method http get
	/// </summary>
	/// <param name="url">url for get</param>
	/// <returns>return response</returns>
	public static string HttpGet(string url)
	{
		HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
		req.Method = WebRequestMethods.Http.Get;
		string result = null;
		using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
		{
			StreamReader reader = new StreamReader(resp.GetResponseStream());
			result = reader.ReadToEnd();
		}

		////Request.CheckError(result);

		return result;
	}

	/// <summary>
	/// method http post
	/// </summary>
	/// <typeparam name="T">object T</typeparam>
	/// <param name="url">url for post</param>
	/// <param name="parameters">parameters post</param>
	/// <returns>return object T</returns>
	public static T HttpPost<T>(string url, Dictionary<string, string> parameters)
	{
		string result = Request.HttpPost(url, parameters);
		var serializer = new JavaScriptSerializer();
		var deserializedResult = serializer.Deserialize<T>(result);
		return deserializedResult;
	}

	/// <summary>
	/// method http post
	/// </summary>
	/// <param name="url">url for post</param>
	/// <param name="parameters">parameters post</param>
	/// <returns>return response</returns>
	public static string HttpPost(string url, Dictionary<string, string> parameters)
	{
		HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest;
		req.Method = WebRequestMethods.Http.Post;
		req.ContentType = "application/x-www-form-urlencoded";

		string queryString = string.Join("&", parameters.Select(x => x.Key + "=" + HttpUtility.UrlEncode(x.Value)));

		// Encode the parameters as form data:
		byte[] formData = UTF8Encoding.UTF8.GetBytes(queryString);
		req.ContentLength = formData.Length;

		// Send the request:
		using (Stream post = req.GetRequestStream())
		{
			post.Write(formData, 0, formData.Length);
		}

		// Pick up the response:
		string result = null;
		using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
		{
			StreamReader reader = new StreamReader(resp.GetResponseStream());
			result = reader.ReadToEnd();
		}

		////Request.CheckError(result);

		return result;
	}

	/// <summary>
	/// verifica se il result ha un messaggio di errore. Nel caso getta un'eccezione
	/// </summary>
	/// <param name="result">result da analizzare</param>
	////private static void CheckError(string result)
	////{
	////    ErrorRest.ErrorRest error = JsonConvert.DeserializeObject<ErrorRest.ErrorRest>(result);
	////    if (error.error != null)
	////    {
	////        string details = string.Join(",", error.error.details);
	////        throw new Exception(string.Format("Error code: {0} - Messaggio: {1}{2}", error.error.code, error.error.message, string.IsNullOrEmpty(details) ? string.Empty : string.Format("- Dettagli: {0}", details)));
	////    }
	////}
}
ShaikhRizuan
New Contributor III

Thanks Domenico.. It helps!!

0 Kudos