ESRI Admin API

      ArcGIS从10.1开始, 开放了基于Rest的Admin API。通过这个API,可以重构ArcGIS Server Manager。

      很简单,一句话,用Http请求去访问指定的Url。Url为管理员目录(http://<服务器名称>:6080/arcgis/admin)+操作地址+Token,如停止服务的URL为:

      http://localhost:6080/arcgis/admin/services/SampleWorldCities.MapServer/stop?f=json&token=

具体代码如下:

1)获取Token

private void GenerateAGSToken(string username, string password, string adminurl)
        {
            try
            {
                string urlGenerateToken = string.Format("{0}/generateToken", adminurl);
                string credential = string.Format("username={0}&password={1}&client=requestip&f=json", username, password);

                //以POST方式获得Token
                GetResult(urlGenerateToken, credential);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

   private void GetResult(string url, string postContent)
        {
            HttpWebRequest request = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                this.content =postContent;
                request.ContentLength = Encoding.UTF8.GetBytes(postContent).Length;
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "text/plain";
                request.Method = "POST";
                IAsyncResult asyresult = request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
               
   }
            catch
            {
                throw;
            }
        }

        private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            Stream postStream = request.EndGetRequestStream(asynchronousResult);

            // Write to the request stream.
            using (StreamWriter writer = new StreamWriter(postStream))
            {
                writer.Write(this.content);
                //writer.Flush();
                writer.Close();
            }
            //postStream.Close();
            request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);

        }
     
        private  void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            string responseString = streamRead.ReadToEnd();


            JObject jObj = JObject.Parse(responseString);
            this.token =(string ) jObj["token"];
           
            // Close the stream object
            streamResponse.Close();
            streamRead.Close();

            // Release the HttpWebResponse
            response.Close();
           
        }

2)停止服务

  public void StopService(string serviceName, ServiceType serviceType)
        {
            try
            {
                string serviceUrl = serviceName + "." + Enum.GetName(typeof(ServiceType), serviceType) + "/stop";
                this.GetResult(serviceUrl, "f=json&token=" + token);
                
            }
            catch
            {
                //return false;
            }
        }


其中JObject jObj = JObject.Parse(responseString);
            this.token =(string ) jObj["token"];

使用的是JSON.NET 我的开发环境是VS2010+Silverlight 5,所以需要使用的版本是V4.5。如果是非Silverlight环境,直接用NuGet下载最新版就可。


你可能感兴趣的:(ESRI Admin API)