NND,openlayers跨域访问geoserver居然要用到代理,貌似Felx就不用,用C#编写代理网上普遍是这样的:
public class GeoServerProxy1 : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.Request.QueryString["URL"] != null) { string url = ""; url = context.Request.QueryString["URL"].ToString(); HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(url); loHttp.Timeout = 10000; // 10 secs loHttp.UserAgent = "Web Client"; HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse(); Encoding enc = Encoding.GetEncoding(65001); StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc); string lcHtml = loResponseStream.ReadToEnd(); context.Response.Write(lcHtml); loWebResponse.Close(); loResponseStream.Close(); } } public bool IsReusable { get { return false; } } }
但是上面这种情况,没有考虑post,不适用于openlayers官方的wfs协议访问,但是可以用于http查询方式的访问,例如:
vectorLayer = new OpenLayers.Layer.Vector("覆冰点", { protocol: new OpenLayers.Protocol.HTTP({ //url: "http://10.180.80.206:9000/geoserver/sdgis/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=sdgis:V_YL_24&maxFeatures=50", //url: "http://10.180.80.206:9000/geoserver/sdgis/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=sdgis:GIS_WEATHER_FORECAST&maxFeatures=50", url: "http://10.180.80.206:9000/geoserver/sdgis/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=sdgis:V_FBJC_PT&maxFeatures=50", format: new OpenLayers.Format.GML() }), styleMap: new OpenLayers.StyleMap({ 'default': { strokeColor: "#00FF00", strokeOpacity: 1, strokeWidth: 2, fillColor: "#FF0000", fillOpacity: 1, pointRadius: 6, strokeDashstyle: 'longdashdot', pointerEvents: "visiblePainted", externalGraphic: "snow.gif", graphicWidth: 12, graphicHeight: 12, //label: "name: ${OWNER}, age: ${FLAGS}", fontColor: "${favColor}", fontSize: "12px", fontFamily: "Courier New, monospace", fontWeight: "bold", labelAlign: "${align}", labelXOffset: "${xOffset}", labelYOffset: "${yOffset}" } }), renderers: renderer, strategies: [new OpenLayers.Strategy.Fixed()] });
我们,先来看看openlayers官方wfs协议是怎么访问的:
var fbjcLayer = new OpenLayers.Layer.Vector("FBJC_PT", { strategies: [new OpenLayers.Strategy.BBOX()], protocol: new OpenLayers.Protocol.WFS({ version: "1.1.0", url: "http://10.180.80.206:9000/geoserver/wfs", featureType: "sdgis:V_FBJC_PT", featureNS: "sdgid" }), renderers: OpenLayers.Layer.Vector.prototype.renderers });
这是一个post请求,我们修改代理程序来支持,如果用网上流传的代理程序会报parameter missing异常,下面是改进后的C#代理:
public class GeoServerProxy1 : IHttpHandler { public void ProcessRequest(HttpContext context) { if (string.IsNullOrEmpty(context.Request["URL"])) return; HttpWebRequest request = (HttpWebRequest) WebRequest.Create(context.Request["URL"]); request.UserAgent = context.Request.UserAgent; request.ContentType = context.Request.ContentType; request.Method = context.Request.HttpMethod; byte[] trans = new byte[1024]; int offset = 0; int offcnt = 0; if (request.Method.ToUpper() == "POST") { Stream nstream = request.GetRequestStream(); while (offset < context.Request.ContentLength) { offcnt = context.Request.InputStream.Read(trans, offset, 1024); if (offcnt > 0) { nstream.Write(trans, 0, offcnt); offset += offcnt; } } nstream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //Encoding enc = Encoding.GetEncoding(65001); context.Response.ContentType = response.ContentType; StreamReader loResponseStream = new StreamReader(response.GetResponseStream()); string lcHtml = loResponseStream.ReadToEnd(); context.Response.Write(lcHtml); response.Close(); loResponseStream.Close(); } public bool IsReusable { get { return false; } } }