GeoServer用c#创建工作区、上传数据存储、发布图层

整合代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace GeoTest.Application.Utility.Tools
{
    public class GeoHelper
    {
        HttpGeoServerRest _http = new HttpGeoServerRest();
        private static string _server = Config.GetValue("GeoServerUrl");
        #region 操作工作区 WorkSpeceNoExistThenAdd(string name)
        /// <summary>
        /// 判断是否存在工作区
        /// </summary>
        /// <param name="workspaceName"></param>
        /// <returns></returns>
        public bool WorkSpaceExist(string workSpaceName)
        {
            try
            {
                string url = _server + "rest/workspaces/" + workSpaceName + ".json";
                //检查Store
                String workspace = _http.CreateGet(url);
                if (workspace.IndexOf("No such workspace") > -1)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 查看工作区是否存在,不存在则添加
        /// </summary>
        /// <param name="workSpaceName"></param>
        /// <returns></returns>
        public bool WorkSpeceNoExistThenAdd(string workSpaceName) {
            if (WorkSpaceExist(workSpaceName)) {
                return true;
            }
            return AddWorkSpace(workSpaceName);
        }

        /// <summary>
        /// 添加工作区
        /// </summary>
        /// <param name="workSpaceName"></param>
        /// <returns></returns>
        public bool AddWorkSpace(string workSpaceName) {
            try
            {
                string url = _server + "rest/workspaces";
                PostWorkSpaces workSpaces = new PostWorkSpaces();
                workSpaces.workspace = new PostWorkSpaces.WorkSpaceObj() { name = workSpaceName };
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(workSpaces);
                String newWorkSpaceName = _http.CreatePost(url, json, "text/json");
                if (newWorkSpaceName.Equals(workSpaceName))
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        private class PostWorkSpaces {
            public WorkSpaceObj workspace { get; set; }
            public class WorkSpaceObj
            {
                public string name { get; set; }
            }
        }
        #endregion

        public bool AddCoverageStores(string workSpacesName,string coverageStoresName,string url, string type= "GeoTIFF", string enabled= "true") {
            if (!WorkSpeceNoExistThenAdd(workSpacesName)) {
                throw new Exception("添加工作区失败");
            }
            PostCoverageStores coverageStores = new PostCoverageStores();
            coverageStores.coverageStore = new PostCoverageStores.CoverageStoreObj()
            {
                name=coverageStoresName,
                url= "file://"+url,
                workspace=new PostCoverageStores.WorkspaceObj() { 
                    name=workSpacesName,
                    link="string"
                }
            };
            string rqjson = Newtonsoft.Json.JsonConvert.SerializeObject(coverageStores);
            String newCoverageStoreName = _http.CreatePost(_server + "rest/workspaces/"+workSpacesName+"/coveragestores", rqjson, "text/json");
            if (newCoverageStoreName.Equals(coverageStoresName)) {
                return true;
            }
            return false;
        }

        private class PostCoverageStores { 
            public CoverageStoreObj coverageStore { get; set; }
            public class CoverageStoreObj {
                public string name { get; set; }
                public string type { get; set; } = "GeoTIFF";
                public bool enabled { get; set; } = true;
                public string url { get; set; }
                public WorkspaceObj workspace { get; set; }
            }
            public class WorkspaceObj { 
                public string name { get; set; }
                public string link { get; set; }
            }
        }

        public bool ReleaseCoverages(string workSpacesName, string coverageStoresName, string coverageName, List<string> supportedFormatsStringList=null,string title="",string nativeCRSDollar="", string srs= "EPSG:3395", string nativeFormat= "GeoTIFF")
        {
            if (string.IsNullOrEmpty(nativeCRSDollar)) {
                string[] srsArray = srs.Split(':');
                if (srsArray.Length != 2)
                    return false;
                nativeCRSDollar = "PROJCS[\"NAD27 / UTM zone 13N\", \n  GEOGCS[\"NAD27\", \n    DATUM[\"North American Datum 1927\", \n      SPHEROID[\"Clarke 1866\", 6378206.4, 294.9786982138982, AUTHORITY[\"EPSG\",\"7008\"]], \n      TOWGS84[2.478, 149.752, 197.726, 0.526, -0.498, 0.501, 0.685], \n      AUTHORITY[\"EPSG\",\"6267\"]], \n    PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]], \n    UNIT[\"degree\", 0.017453292519943295], \n    AXIS[\"Geodetic longitude\", EAST], \n    AXIS[\"Geodetic latitude\", NORTH], \n    AUTHORITY[\"EPSG\",\"4267\"]], \n  PROJECTION[\"Transverse_Mercator\", AUTHORITY[\"EPSG\",\"9807\"]], \n  PARAMETER[\"central_meridian\", -105.0], \n  PARAMETER[\"latitude_of_origin\", 0.0], \n  PARAMETER[\"scale_factor\", 0.9996], \n  PARAMETER[\"false_easting\", 500000.0], \n  PARAMETER[\"false_northing\", 0.0], \n  UNIT[\"m\", 1.0], \n  AXIS[\"Easting\", EAST], \n  AXIS[\"Northing\", NORTH], \n  AUTHORITY[\""+ srsArray[0]+ "\",\""+srsArray[1]+"\"]]";
            }
            if (string.IsNullOrEmpty(title))
                title = workSpacesName + ":" + coverageStoresName + ":" + coverageName;
            if (supportedFormatsStringList == null) {
                supportedFormatsStringList=new List<string>() { "GEOTIFF", "GIF", "PNG", "JPEG", "TIFF" };
            }
            PostCoverage coverage = new PostCoverage();
            coverage.coverage = new PostCoverage.CoverageObj()
            {
                name= coverageName,
                nativeFormat= nativeFormat,
                srs=srs,
                title=title,
                supportedFormats=new PostCoverage.SupportedFormatsObj() { 
                    CoverageSupportedFormatsObjStringListField8659746s87d4f= supportedFormatsStringList
                },
                nativeCRS=new PostCoverage.NativeCRSObj() {
                    CoverageNativeCRSObjDollarFieldS56D46FS1DG31S56D4F= nativeCRSDollar,
                    @class= "projected"
                }
            };
            string rqjson = Newtonsoft.Json.JsonConvert.SerializeObject(coverage);
            rqjson=rqjson.Replace("CoverageSupportedFormatsObjStringListField8659746s87d4f", "string");
            rqjson=rqjson.Replace("CoverageNativeCRSObjDollarFieldS56D46FS1DG31S56D4F", "$");
            String newCoverageName = _http.CreatePost(_server + "rest/workspaces/"+ workSpacesName + "/coveragestores/"+ coverageStoresName + "/coverages", rqjson, "application/json");
            if (coverageName.Equals(newCoverageName))
            {
                return true;
            }
            return false;
        }

        public string ReleaseCoverages(string workSpacesName, string url) {
            GetCoverageStoresModel stores = GetCoverageStores(workSpacesName);
            int count = stores.coverageStores.coverageStore.Count + 1;
            string storeName = workSpacesName + "_Store" + count;
            string coverageName = workSpacesName + "_Store" + count+ "_Coverage1";
            if (AddCoverageStores(workSpacesName, storeName, url)) {
                if (ReleaseCoverages(workSpacesName, storeName, coverageName)) {
                    return coverageName;
                }
            }
            return "";
        }

        public GetCoverageStoresModel GetCoverageStores(string workSpaceName) {
            try
            {
                string url = _server + "rest/workspaces/"+ workSpaceName + "/coveragestores";
                //检查Store
                string coverageStoresJsonStr = _http.CreateGet(url);
                return Newtonsoft.Json.JsonConvert.DeserializeObject<GetCoverageStoresModel>(coverageStoresJsonStr);
            }
            catch (Exception ex)
            {
                return new GeoHelper.GetCoverageStoresModel();
            }
        }

        private class PostCoverage {
            public CoverageObj coverage { get; set; }
            public class CoverageObj { 
                public string name { get; set; }
                public string nativeFormat { get; set; }
                public string srs { get; set; }
                public string title { get; set; }
                public bool enabled { get; set; } = true;
                public SupportedFormatsObj supportedFormats { get; set; }
                public NativeCRSObj nativeCRS { get; set; }
            }
            public class SupportedFormatsObj { 
                public List<string> CoverageSupportedFormatsObjStringListField8659746s87d4f { get; set; }
            }
            public class NativeCRSObj { 
                public string CoverageNativeCRSObjDollarFieldS56D46FS1DG31S56D4F { get;set;}
                public string @class { get; set; }
            }
        }

        public class GetCoverageStoresModel {
            public CoverageStoresObj coverageStores { get; set; } = new CoverageStoresObj();
            public class CoverageStoresObj {
                public List<CoverageStoreListObj> coverageStore { get; set; } = new List<CoverageStoreListObj>();
            }
            public class CoverageStoreListObj {
                public string name { get; set; }
                public string href { get; set; }
            }
        }
    }

    public class HttpGeoServerRest
    {
        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
    }
}

使用

GeoHelper geoHelper = new GeoHelper();
string workPlace = "DemoWorkPlace";
geoHelper.ReleaseCoverages(workPlace, "文件路径");

下面是介绍,都是从上面代码分段提取出的,可以选择使用

一、创建工作区

		HttpGeoServerRest _http = new HttpGeoServerRest();
        private static string _server = Config.GetValue("GeoServerUrl");//http://localhost:8080/geoserver
		/// <summary>
        /// 判断是否存在工作区
        /// </summary>
        /// <param name="workspaceName"></param>
        /// <returns></returns>
        public bool WorkSpaceExist(string workSpaceName)
        {
            try
            {
                string url = _server + "rest/workspaces/" + workSpaceName + ".json";
                //检查Store
                String workspace = _http.CreateGet(url);
                if (workspace.IndexOf("No such workspace") > -1)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 查看工作区是否存在,不存在则添加
        /// </summary>
        /// <param name="workSpaceName"></param>
        /// <returns></returns>
        public bool WorkSpeceNoExistThenAdd(string workSpaceName) {
            if (WorkSpaceExist(workSpaceName)) {
                return true;
            }
            return AddWorkSpace(workSpaceName);
        }

        /// <summary>
        /// 添加工作区
        /// </summary>
        /// <param name="workSpaceName"></param>
        /// <returns></returns>
        public bool AddWorkSpace(string workSpaceName) {
            try
            {
                string url = _server + "rest/workspaces";
                PostWorkSpaces workSpaces = new PostWorkSpaces();
                workSpaces.workspace = new PostWorkSpaces.WorkSpaceObj() { name = workSpaceName };
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(workSpaces);
                String newWorkSpaceName = _http.CreatePost(url, json, "text/json");
                if (newWorkSpaceName.Equals(workSpaceName))
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        private class PostWorkSpaces {
            public WorkSpaceObj workspace { get; set; }
            public class WorkSpaceObj
            {
                public string name { get; set; }
            }
        }

二、上传数据存储

		public bool AddCoverageStores(string workSpacesName,string coverageStoresName,string url, string type= "GeoTIFF", string enabled= "true") {
            if (!WorkSpeceNoExistThenAdd(workSpacesName)) {
                throw new Exception("添加工作区失败");
            }
            PostCoverageStores coverageStores = new PostCoverageStores();
            coverageStores.coverageStore = new PostCoverageStores.CoverageStoreObj()
            {
                name=coverageStoresName,
                url= "file://"+url,
                workspace=new PostCoverageStores.WorkspaceObj() { 
                    name=workSpacesName,
                    link="string"
                }
            };
            string rqjson = Newtonsoft.Json.JsonConvert.SerializeObject(coverageStores);
            String newCoverageStoreName = _http.CreatePost(_server + "rest/workspaces/"+workSpacesName+"/coveragestores", rqjson, "text/json");
            if (newCoverageStoreName.Equals(coverageStoresName)) {
                return true;
            }
            return false;
        }

        private class PostCoverageStores { 
            public CoverageStoreObj coverageStore { get; set; }
            public class CoverageStoreObj {
                public string name { get; set; }
                public string type { get; set; } = "GeoTIFF";
                public bool enabled { get; set; } = true;
                public string url { get; set; }
                public WorkspaceObj workspace { get; set; }
            }
            public class WorkspaceObj { 
                public string name { get; set; }
                public string link { get; set; }
            }
        }

三、发布图层

		public bool ReleaseCoverages(string workSpacesName, string coverageStoresName, string coverageName, List<string> supportedFormatsStringList=null,string title="",string nativeCRSDollar="", string srs= "EPSG:3395", string nativeFormat= "GeoTIFF")
        {
            if (string.IsNullOrEmpty(nativeCRSDollar)) {
                string[] srsArray = srs.Split(':');
                if (srsArray.Length != 2)
                    return false;
                nativeCRSDollar = "PROJCS[\"NAD27 / UTM zone 13N\", \n  GEOGCS[\"NAD27\", \n    DATUM[\"North American Datum 1927\", \n      SPHEROID[\"Clarke 1866\", 6378206.4, 294.9786982138982, AUTHORITY[\"EPSG\",\"7008\"]], \n      TOWGS84[2.478, 149.752, 197.726, 0.526, -0.498, 0.501, 0.685], \n      AUTHORITY[\"EPSG\",\"6267\"]], \n    PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]], \n    UNIT[\"degree\", 0.017453292519943295], \n    AXIS[\"Geodetic longitude\", EAST], \n    AXIS[\"Geodetic latitude\", NORTH], \n    AUTHORITY[\"EPSG\",\"4267\"]], \n  PROJECTION[\"Transverse_Mercator\", AUTHORITY[\"EPSG\",\"9807\"]], \n  PARAMETER[\"central_meridian\", -105.0], \n  PARAMETER[\"latitude_of_origin\", 0.0], \n  PARAMETER[\"scale_factor\", 0.9996], \n  PARAMETER[\"false_easting\", 500000.0], \n  PARAMETER[\"false_northing\", 0.0], \n  UNIT[\"m\", 1.0], \n  AXIS[\"Easting\", EAST], \n  AXIS[\"Northing\", NORTH], \n  AUTHORITY[\""+ srsArray[0]+ "\",\""+srsArray[1]+"\"]]";
            }
            if (string.IsNullOrEmpty(title))
                title = workSpacesName + ":" + coverageStoresName + ":" + coverageName;
            if (supportedFormatsStringList == null) {
                supportedFormatsStringList=new List<string>() { "GEOTIFF", "GIF", "PNG", "JPEG", "TIFF" };
            }
            PostCoverage coverage = new PostCoverage();
            coverage.coverage = new PostCoverage.CoverageObj()
            {
                name= coverageName,
                nativeFormat= nativeFormat,
                srs=srs,
                title=title,
                supportedFormats=new PostCoverage.SupportedFormatsObj() { 
                    CoverageSupportedFormatsObjStringListField8659746s87d4f= supportedFormatsStringList
                },
                nativeCRS=new PostCoverage.NativeCRSObj() {
                    CoverageNativeCRSObjDollarFieldS56D46FS1DG31S56D4F= nativeCRSDollar,
                    @class= "projected"
                }
            };
            string rqjson = Newtonsoft.Json.JsonConvert.SerializeObject(coverage);
            rqjson=rqjson.Replace("CoverageSupportedFormatsObjStringListField8659746s87d4f", "string");
            rqjson=rqjson.Replace("CoverageNativeCRSObjDollarFieldS56D46FS1DG31S56D4F", "$");
            String newCoverageName = _http.CreatePost(_server + "rest/workspaces/"+ workSpacesName + "/coveragestores/"+ coverageStoresName + "/coverages", rqjson, "application/json");
            if (coverageName.Equals(newCoverageName))
            {
                return true;
            }
            return false;
        }

        public string ReleaseCoverages(string workSpacesName, string url) {
            GetCoverageStoresModel stores = GetCoverageStores(workSpacesName);
            int count = stores.coverageStores.coverageStore.Count + 1;
            string storeName = workSpacesName + "_Store" + count;
            string coverageName = workSpacesName + "_Store" + count+ "_Coverage1";
            if (AddCoverageStores(workSpacesName, storeName, url)) {
                if (ReleaseCoverages(workSpacesName, storeName, coverageName)) {
                    return coverageName;
                }
            }
            return "";
        }

        public GetCoverageStoresModel GetCoverageStores(string workSpaceName) {
            try
            {
                string url = _server + "rest/workspaces/"+ workSpaceName + "/coveragestores";
                //检查Store
                string coverageStoresJsonStr = _http.CreateGet(url);
                return Newtonsoft.Json.JsonConvert.DeserializeObject<GetCoverageStoresModel>(coverageStoresJsonStr);
            }
            catch (Exception ex)
            {
                return new GeoHelper.GetCoverageStoresModel();
            }
        }

        private class PostCoverage {
            public CoverageObj coverage { get; set; }
            public class CoverageObj { 
                public string name { get; set; }
                public string nativeFormat { get; set; }
                public string srs { get; set; }
                public string title { get; set; }
                public bool enabled { get; set; } = true;
                public SupportedFormatsObj supportedFormats { get; set; }
                public NativeCRSObj nativeCRS { get; set; }
            }
            public class SupportedFormatsObj { 
                public List<string> CoverageSupportedFormatsObjStringListField8659746s87d4f { get; set; }
            }
            public class NativeCRSObj { 
                public string CoverageNativeCRSObjDollarFieldS56D46FS1DG31S56D4F { get;set;}
                public string @class { get; set; }
            }
        }

        public class GetCoverageStoresModel {
            public CoverageStoresObj coverageStores { get; set; } = new CoverageStoresObj();
            public class CoverageStoresObj {
                public List<CoverageStoreListObj> coverageStore { get; set; } = new List<CoverageStoreListObj>();
            }
            public class CoverageStoreListObj {
                public string name { get; set; }
                public string href { get; set; }
            }
        }
    }

HttpGeoServerRest这个方法类是这个大佬写的
具体代码如下

    public class HttpGeoServerRest
    {
        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
    }

吐槽一下geoserver这玩意的官方文档简直读不了,我都是穷举法一个一个试出来的。头要秃了我才20多岁啊。

个人开发小记 2021-12-30 下午(其实是前几天弄好的,太懒了不想写先)

posted @ 2021-12-30 16:27  清水截  阅读(103)  评论(0)    收藏  举报  来源