Arcgis Server發佈的地圖服務地址默認端口號是6080,假設本機上只對80端口做了外網映射,在IIS中部署了一個網站綁定了80端口,那麼網站中某個頁面通過arcgis api for js 加載Arcgis Server發佈的地圖服務就無法加載出來了。
在此情況下,可以將Arcgis Server發佈的地圖服務地址Url設爲網站的特定地址,然後通過攔截器攔截特定地址,在攔截器中用HttpWebRequest訪問localhost:6080,將HttpWebResponse響應流寫入Response.OutputStream中。
(其實就是類似於反向代理,將地圖服務接口的請求轉發到Arcgis Server 的Web服務器)
例:
服務器局域網地址爲:192.168.1.100
服務器80端口映射外網地址爲:61.135.169.125
服務器在IIS中部署網站綁定的端口爲:80
服務器Arcgis Server發佈的地圖服務地址端口爲:6080
現在如果想通過arcgis js api 加載 服務接口 192.168.1.100:6080/arcgis/rest/services/test/service1/FeatureServer/0;
如果在內網中訪問,下面的代碼地圖加載出來沒問題。
var myFeatureLayer0 = new FeatureLayer("http://192.168.1.100:6080/arcgis/rest/services/test/service1/FeatureServer/0", { "id": "myFeatureLayer0", "opacity": 0.5, "outFields": ["*"], "mode": FeatureLayer.MODE_ONDEMAND });
但是如果在外網訪問因爲無法訪問6080端口,所以必須通過80端口的網站通過模擬HttpWebRequest模擬請求 192.168.1.100:6080/arcgis/rest/services/test/service1/FeatureServer/0 ,然後返回客戶端
html頁面代碼:
var myFeatureLayer0 = new FeatureLayer("http://61.135.169.125/arcgis/rest/services/test/service1/FeatureServer/0", { "id": "myFeatureLayer0", "opacity": 0.5, "outFields": ["*"], "mode": FeatureLayer.MODE_ONDEMAND });
HttpModule攔截器代碼:
public class ArcgsiServerUrlMappingModule : IHttpModule { public void Init(HttpApplication application) { application.BeginRequest += this.Application_BeginRequest; //註冊事件 } private void Application_BeginRequest(Object source, EventArgs e) { HttpApplication application = (HttpApplication)source; string url = application.Context.Request.RawUrl; if (url.StartsWith("/arcgis/rest/services")) { url = "http://192.168.1.100:6080" + url; HttpWebRequest hwRequest = (HttpWebRequest)WebRequest.Create(url); hwRequest.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705"; hwRequest.Accept = "*/*"; hwRequest.KeepAlive = true; hwRequest.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5"); using (HttpWebResponse hwResponse = (HttpWebResponse)hwRequest.GetResponse()) { Stream hwStream = hwResponse.GetResponseStream(); byte[] buffer = new byte[1024]; int count = 0; while ((count = hwStream.Read(buffer, 0, buffer.Length)) > 0) { application.Context.Response.OutputStream.Write(buffer, 0, count); } application.Context.Response.ContentType = hwResponse.ContentType; application.Context.Response.End(); } } } public void Dispose() { } }
HttpModule需要在Web.config中的<system.webServer>節點配置相關節點才能起到攔截請求的作用,在次不做說明。
附設計流程圖