Arcgis api for javascript学习笔记 - 不改变默认端口(6080)情况下,外网访问Arcgis Server 发布的接口

时间:2022-04-04 10:18:28

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[];
int count = ;
while ((count = hwStream.Read(buffer, , buffer.Length)) > )
{
application.Context.Response.OutputStream.Write(buffer, , count);
}
application.Context.Response.ContentType = hwResponse.ContentType;
application.Context.Response.End();
}
}
} public void Dispose() { }
}
HttpModule需要在Web.config中的<system.webServer>节点配置相关节点才能起到拦截请求的作用,在次不做说明。

附设计流程图

Arcgis api for javascript学习笔记 - 不改变默认端口(6080)情况下,外网访问Arcgis Server 发布的接口