proxy 代理

转自   https://blog.csdn.net/u011365716/article/details/70207539

为什么要使用代理proxy?

答:未配置代理时server发送请求是用get的方式,请求跨域访问或者URL长度超过浏览器限制时,这就需要一个代理文件proxy来转发请求。Arcgis Server 10.0 由于不支持CORS,需要配置代理才能在前端js 程序中范围进行操作,而有时候未配置代理也未出错,例如:用extent进行空间查询 后不报错是因为选择extent 请求的参数字符串较短,jsapi 可以通过jsonp方法绕过跨域限制,但是一旦前端参数较多,例如用circle进行空间查询,可以就无法通过jsonp跨域,这个时候如果要使用 10.0的 Identify task 就必须配置代理;还有Arcgis Server 10.2的打印功能可能也需要配置代理才行。

 

下载地址:https://github.com/Esri/resource-proxy/releases(注意:不同版本的Arcgis Server应下载对应版本的proxy文件,而该页面中的V1.1.0版本其实是支持10.2和10.3版本的,V1.0版本支持10.0版本,下载后解压可以看到有3中类型:DotNet、Java、PHP的代理proxy文件,此处以Java为例)

 

修改文件:

V1.1.0版本:该文件下载下来后需要修改proxy.jsp内容,全局搜索“<>”将对应的类型写入,例如:文件中有HashMap<>,未写类型,可能就需要修改为HashMap<String,String>;还需要修改文件“WEB-INF/classes/proxy.config”文件中的“serverUrl”标签的url属性,修改为要调用Arcgis Server的地址,例如:“http://192.168.202.143:9797/arcgis/rest/services”)


(备注:若有多个不同域的服务地址,则需配置多个不同域的服务地址)

 

V1.0版本:不需要修改proxy.jsp内容,但该版本也需要修改 “WEB-INF/Classes/proxy.config”文件,修改同V1.1.0版本一致。

 

如何部署?

修改完成后,将“Java"整个文件拷贝到Tomcat的webapps文件下,启动Tomcat。

 

如何测试是否成功?

访问:http:// 192.168.0.207:9391/Java/proxy.jsp?ping,会看到:

{ "ProxyVersion": "1.1.0", "Configuration File":"OK", "Log File": "OK"},然后再访问:http:// 192.168.0.207:9391/Java/proxy.jsp?http://192.168.202.143:9797/arcgis/rest/services/?f=pjson,如果看到指定Arcgis Server目录下的json数据,如

{"currentVersion":10.21,"folders":["Utilities","XKMSLFH"],"services":[{"name":"DOM2013","type":"ImageServer"}]},则到目前为止,代理配置成功!

 

如何在项目中使用?

Inorder for your application to route requests through the proxy you must addcode to your application to define the location of the proxy. If all requestsin your application will use the same proxy you can specify the proxy locationusing proxyUrl. You can also specify whether or not the proxy should always beused for communication using alwaysUseProxy

下面2行代码必须在项目中出现才能使该项目使用代理:

esri.config.defaults.io.proxyUrl= "http://192.168.0.207:9391/Java/proxy.jsp";

esri.config.defaults.io.alwaysUseProxy= false;

 

In the code above,esriConfig refers to the object returned by the esri/config module.

Another option isto setup a proxy rule that defines the proxy for a set of resources with the sameURL prefix. If the request URL matches a rule, then the request will be routedthrough the proxy. To define a proxy rule specify the url for the proxy and theprefix for the resources that need to be accessed through the proxy.

下面的代码可不用出现在项目中,不知道具体有什么影响:

esri.urlUtils.addProxyRule({

       urlPrefix:"route.arcgis.com",

        proxyUrl:"http://192.168.0.207:9391/Java/proxy.jsp"

});


猜你喜欢

转载自blog.csdn.net/qq_36375770/article/details/80057430