格式化RestSharp请求的ESRI地理编码器

本文关键字:编码器 ESRI RestSharp 请求 格式化 | 更新日期: 2023-09-27 17:54:06

我有一些代码,使用RestSharp格式化JSON请求以访问ESRI地理编码API。代码可以工作,但我想知道是否有更好的方法将请求转换为正确的格式,这里是我所拥有的示例,下面是请求应该看起来像的示例。

request = new RestRequest("geocodeAddresses", Method.POST);
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
        //Format the request properly
        var attributes = new Dictionary<string, object>();
        attributes.Add("OBJECTID", address.Address);
        attributes.Add("Address", address.Address);
        attributes.Add("City", address.City);
        attributes.Add("Region", address.State);
        attributes.Add("Postal", address.ZipCode);
        JsonObject attributesObj = new JsonObject();
        foreach (var parms in attributes)
        {
            attributesObj.Add(parms);
        }

        JsonObject recordsObj = new JsonObject();
        recordsObj.Add("attributes", attributesObj);
        JsonArray EsriRequest = new JsonArray();
        EsriRequest.Add(recordsObj);
        JsonObject addressObj = new JsonObject();
        addressObj.Add("records", EsriRequest);


        request.AddParameter("addresses",
            addressObj.ToString());
        request.AddParameter("token", esriToken.ToString());
        request.AddParameter("f", "json");
        request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
        IRestResponse<EsriAddress> responseData = client.Execute<EsriAddress>(request);

请求输出示例:

http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/geocodeAddresses?addresses={"records":[{"attributes":{"OBJECTID":1,"Address":"380 New York St.","City":"Redlands","Region":"CA","Postal":"92373"}},{"attributes":{"OBJECTID":2,"Address":"1 World Way","City":"Los Angeles","Region":"CA","Postal":"90045"}}]}&sourceCountry=USA&token=<YOUR TOKEN>&f=pjson

我目前只发送一个地址,但理论上api可以一次接收多个地址。

格式化RestSharp请求的ESRI地理编码器

下面是我如何发送一批地址到ArcGIS REST API中的geocodeAddresses方法。我没有使用RestSharp,只是HttpClient:

string token = GetToken();
string url = "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/geocodeAddresses"; 
using (var client = new HttpClient())
{
    using (var content = new MultipartFormDataContent())
    {
        var values = new[]
        {
            new KeyValuePair<string, string>("token", token),
            new KeyValuePair<string, string>("forStorage", "true"),
            new KeyValuePair<string, string>("MaxBatchSize", "1000"),
            new KeyValuePair<string, string>("outFields", "*"),
            new KeyValuePair<string, string>("f", "json"),
            new KeyValuePair<string, string>("addresses", inputJson)  // json string containing an array of a complex type
        };
        foreach (var keyValuePair in values)        
            content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
        var response = await client.PostAsync(url, content);
        Task<string> responseString = response.Content.ReadAsStringAsync();
        string outputJson = await responseString; 
    }
}