GeoServer REST支持HTTP的Get\POST\PUT,具体的可以看GeoServer的REST手册,在此不累赘描述。直接切入主题。
C#的HTTP操作类
public class HttpGeoServerRest { // //private static readonly string defaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; private static readonly string defaultUserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; private CookieContainer _cookieContainer = new CookieContainer(); public CookieContainer CookieContainer { set { this._cookieContainer = value; } get { return this._cookieContainer; } } #region Get请求 public string CreateGet(string url) { return CreateGet(url, Encoding.UTF8, null, null); } public string CreateGet(string url, Encoding coding) { return CreateGet(url, coding, null, null); } public string CreateGet(string url, Encoding coding, string userAgent, CookieCollection cookies) { HttpWebRequest request = null; HttpWebResponse response = null; request = WebRequest.Create(url) as HttpWebRequest; request.Method = "GET"; request.KeepAlive = false; //request.ContentType = "application/x-www-form-urlencoded"; //Cookie if (cookies != null) CookieContainer.Add(cookies); request.CookieContainer = CookieContainer; //userAgent if (!string.IsNullOrEmpty(userAgent)) request.UserAgent = userAgent; else request.UserAgent = defaultUserAgent; // 提交请求数据 CredentialCache myCredential = new CredentialCache(); myCredential.Add(new Uri(url), "Basic", new NetworkCredential("admin", "geoserver")); request.Credentials = myCredential; response = request.GetResponse() as HttpWebResponse; System.IO.Stream responseStream = response.GetResponseStream(); System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, coding); string srcString = reader.ReadToEnd(); return srcString; } #endregion #region Post请求 public string CreatePost(string url, string param, string contentType) { if (string.IsNullOrEmpty(contentType)) { contentType="text/xml"; } return CreatePost(url, Encoding.UTF8, param,"POST", contentType, null, null); } public string CreatePut(string url, string param, string contentType) { if (string.IsNullOrEmpty(contentType)) { contentType="text/xml"; } return CreatePost(url, Encoding.UTF8, param,"PUT" , contentType, null, null); } public string CreatePost(string url, Encoding coding, string param,string requestMethod,string contentType, string userAgent, CookieCollection cookies) { HttpWebRequest request = null; HttpWebResponse response = null; request = WebRequest.Create(url) as HttpWebRequest; request.Method = requestMethod; request.KeepAlive = false; request.ContentType = contentType; //Cookie if (cookies != null) CookieContainer.Add(cookies); request.CookieContainer = CookieContainer; //userAgent if (!string.IsNullOrEmpty(userAgent)) request.UserAgent = userAgent; else request.UserAgent = defaultUserAgent; CredentialCache myCredential = new CredentialCache(); myCredential.Add(new Uri(url), "Basic", new NetworkCredential("admin", "geoserver")); request.Credentials = myCredential; byte[] data = Encoding.ASCII.GetBytes(System.Text.RegularExpressions.Regex.Replace(param, "&$", "")); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } // 接收返回的页面 response = request.GetResponse() as HttpWebResponse; System.IO.Stream responseStream = response.GetResponseStream(); System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, coding); string srcString = reader.ReadToEnd(); return srcString; } #endregion }
请注意,代码中的 myCredential.Add(new Uri(url), "Basic", new NetworkCredential("admin", "geoserver"));的用户名与密码替换成自己的。
通过REST判断GeoServer中某个WorkSpace是否存在
private bool workspaceExist(string workspaceName) { //http://localhost:8080/geoserver/rest/workspaces/Lei3Da.json try { string url = ConfigObject.Server + "rest/workspaces/" + workspaceName + ".json"; //检查Store String workspace = _http.CreateGet(url); Console.WriteLine(workspace); if (workspace.IndexOf("No such workspace") > -1) { return false; } return true; } catch (Exception ex) { return false; } }
通过REST判断GeoServer中的某个workspace下面的Store是否存在
private bool storeNameExist(string storeName) { try { string url = ConfigObject.Server + "rest/workspaces/您的某个workspace名称/coveragestores"; //检查Store String storeExist = _http.CreateGet(url + "/" + storeName + ".json"); Console.WriteLine(storeExist); if (storeExist.IndexOf("No such coverage store") > -1) { return false; } return true; } catch (Exception ex) { return false; } }
判断GeoServer中某个图层Layer是否存在
private bool layerNameExist(string layerName) { try { string url = ConfigObject.Server + "rest/layers/" + layerName + ".json"; //检查Store String layerExist = _http.CreateGet(url); Console.WriteLine(layerExist); if (layerExist.IndexOf("No such layer") > -1) { return false; } return true; } catch (Exception ex) { return false; } }