使用.net c#操作GeoServer REST(Get/Post)

使用c# 操作GeoServer REST
Get方式:

 #region 向服务发送Get请求
 public string CreateGet(string url)
        {
            return CreateGet(url, Encoding.UTF8);
        }
 public string CreateGet(string url, Encoding coding)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.KeepAlive = false;

            // 提交请求数据
            CredentialCache myCredential = new CredentialCache();
            myCredential.Add(new Uri(url), "Basic", new NetworkCredential(usernameWfs, passwordWfs));
            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

Post方式:

      #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);
	        }

        public string CreatePost(string url, Encoding coding, string param, string requestMethod, string contentType)
		        {
		            HttpWebRequest request = null;
		            HttpWebResponse response = null;
		            request = WebRequest.Create(url) as HttpWebRequest;
		            request.Method = requestMethod;
		            request.KeepAlive = false;
		            request.ContentType = contentType;
		
		            CredentialCache myCredential = new CredentialCache();
		            myCredential.Add(new Uri(url), "Basic", new NetworkCredential(usernameWfs, passwordWfs));
		            request.Credentials = myCredential;
		
		            byte[] data = Encoding.UTF8.GetBytes(param);
		            using (Stream stream = request.GetRequestStream())
		            {
		                stream.Write(data, 0, data.Length);
		                stream.Close();
		            }
		
		            // 接收返回的页面
		            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

代码中的 usernameWfs, passwordWfs是GeoServer的用户名和密码,替换成自己的即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值