OpenLayer跨域訪問geoserver居然要用到代理,貌似Felx就不用。
Java實現OpenLayers跨域代理程序 http://www.linuxidc.com/Linux/2014-01/95411.htm
用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查詢方式的訪問,例如:
JS代碼:
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()]
});