[转载]C#环境下使用 Get 和Post 的方式访问WFS服务 – Li Minghua的专栏 – 博客频道 – CSDN.NET.
软件开发环境:VS2010
地图服务器:GeoServer 2.3.1
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Net;
using
System.Text;
private void
GetUrlData()
{
WebClient client =
new
WebClient();
string
rUrl =
"http://10.14.1.34:8080/geoserver/wfs?service=WFS&version=1.0.0&request=getFeature&typeName=Shanxi:Toll&PROPERTYNAME=NAME,the_geom&FILTER=<Filter><PropertyIsEqualTo><PropertyName>KIND</PropertyName><Literal>8400</Literal></PropertyIsEqualTo></Filter>"
;
//client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
byte
[] ss = client.DownloadData(
new
Uri(rUrl));
string
result = Encoding.UTF8.GetString(ss);
}
private
void
PostUrlData()
{
//将字符串转换成字节数组, 注意Encoding.UTF8这样才能支持中文查询
byte
[] postBy = Encoding.UTF8.GetBytes(
this
.WfsData());
//初始化WebClient
WebClient webClient =
new
WebClient();
//webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
webClient.Headers.Add(
"Content-Type"
,
"application/xml;charset=UTF-8"
);
webClient.Headers.Add(
"ContentLength"
, postBy.Length.ToString());
//上传数据,返回页面的字节数组
byte
[] responseData = webClient.UploadData(rUrl,
"POST"
, postBy);
// 将返回的将字节数组转换成字符串(HTML);
string
srcString = Encoding.UTF8.GetString(responseData);
}
private
string
WfsData()
{
var xmlPara =
"<?xml version='1.0' encoding='UTF-8'?>"
+
"<wfs:GetFeature service='WFS' version='1.0.0' "
+
"<wfs:Query typeName='Shanxi:Toll' srsName='EPSG:4326'>"
//属性查询
+
"<ogc:And><ogc:PropertyIsLike wildCard='*' singleChar='.' escape='!'>"
+
"<ogc:PropertyName>NAME</ogc:PropertyName>"
+
"<ogc:Literal>*收费站*</ogc:Literal>"
+
"</ogc:PropertyIsLike></ogc:And></ogc:Filter>"
//空间查询
//+ "<ogc:Filter>"
//+ "<ogc:Intersects>"
//+ "<ogc:PropertyName>the_geom</ogc:PropertyName>"
//+ " <gml:Point srsName='http://www.opengis.net/gml/srs/epsg.xml#4326'>"
//+ "<gml:coordinates>111.08158415999999,38.98932696</gml:coordinates>"
//+ "</gml:Point>"
//+ "</ogc:Intersects>"
//+ "</ogc:Filter>"
//后部分节点
+
"</wfs:Query>"
+
"</wfs:GetFeature>"
;
return
xmlPara;
}