openlayers3+Arcgis Server跨域问题 代理解决方式


C#使用代理的方式

后台代码

<%@ WebHandler Language="C#" Class="OpenlayerProxy" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
/// <summary>
/// openlayers的代理类
/// 处理跨域请求
/// </summary>
public class OpenlayerProxy : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        var request = context.Request;
        var response = context.Response;
        var url = request["url"];
        if (string.IsNullOrEmpty(url)) return;

        HttpWebRequest requestproxy = (HttpWebRequest)WebRequest.Create(url);
        requestproxy.UserAgent = context.Request.UserAgent;
        requestproxy.ContentType = context.Request.ContentType;
        HttpWebResponse responseproxy = null;
        Stream stream = null;
        StreamReader reader = null;
        try
        {
            responseproxy = (HttpWebResponse)requestproxy.GetResponse();
            stream = responseproxy.GetResponseStream();
            reader = new StreamReader(stream);
            string text = reader.ReadToEnd();
            response.Write(text);
        }
        catch (Exception ex)
        {
            response.Write("出错了:"+ex.Message);
        }
        finally
        {
            if (reader != null) reader.Close();
            if (stream != null) stream.Close();
            if (responseproxy != null) responseproxy.Close();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }


}

前台调用形式

 var url = "http://服务器ip:6080/arcgis/services/axVector/MapServer/WFSServer?service=WFS&ersion=1.0.0&request=GetFeature&outputFormat=gml2&srsname=EPSG:4544&typename=axVetor:流转项目";
        this.lzxmLayer = new ol.layer.Vector({
            source: new ol.source.Vector({
                url: '/Handler/OpenlayerProxy.ashx?url=' + encodeURIComponent(url),
                format: new ol.format.GML2(),
            })
        });

JAVA使用代理的方法

工具类

package com.thinker.lqhd.controllers.cbjyq;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

public class HttpConnectionUtil {

    // post请求
    public static final String HTTP_POST = "POST";

    // get请求
    public static final String HTTP_GET = "GET";

    // utf-8字符编码
    public static final String CHARSET_UTF_8 = "utf-8";

    // HTTP内容类型。如果未指定ContentType,默认为TEXT/HTML
    public static final String CONTENT_TYPE_TEXT_HTML = "text/xml";

    // HTTP内容类型。相当于form表单的形式,提交暑假
    public static final String CONTENT_TYPE_FORM_URL = "application/x-www-form-urlencoded";

    // 请求超时时间
    public static final int SEND_REQUEST_TIME_OUT = 50000;

    // 将读超时时间
    public static final int READ_TIME_OUT = 50000;

    /**
     * 
     * @param requestType
     *            请求类型
     * @param urlStr
     *            请求地址
     * @param body
     *            请求发送内容
     * @return 返回内容
     */
    public static String requestMethod(String requestType, String urlStr, String body,HttpServletResponse responce) {

        // 是否有http正文提交
        boolean isDoInput = false;
        if (body != null && body.length() > 0)
            isDoInput = true;
        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
        try {
            // 统一资源
            URL url = new URL(urlStr);
            // 连接类的父类,抽象类
            URLConnection urlConnection = url.openConnection();
            // http的连接类
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;

            // 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在
            // http正文内,因此需要设为true, 默认情况下是false;
            if (isDoInput) {
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setRequestProperty("Content-Length", String.valueOf(body.length()));
            }
            // 设置是否从httpUrlConnection读入,默认情况下是true;
            httpURLConnection.setDoInput(true);
            // 设置一个指定的超时值(以毫秒为单位)
            httpURLConnection.setConnectTimeout(SEND_REQUEST_TIME_OUT);
            // 将读超时设置为指定的超时,以毫秒为单位。
            httpURLConnection.setReadTimeout(READ_TIME_OUT);
            // Post 请求不能使用缓存
            httpURLConnection.setUseCaches(false);
            // 设置字符编码
            httpURLConnection.setRequestProperty("Accept-Charset", CHARSET_UTF_8);
            // 设置内容类型
            httpURLConnection.setRequestProperty("Content-Type", CONTENT_TYPE_FORM_URL);
            // 设定请求的方法,默认是GET
            httpURLConnection.setRequestMethod(requestType);

            // 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
            // 如果在已打开连接(此时 connected 字段的值为 true)的情况下调用 connect 方法,则忽略该调用。
            httpURLConnection.connect();

            if (isDoInput) {
                outputStream = httpURLConnection.getOutputStream();
                outputStreamWriter = new OutputStreamWriter(outputStream);
                outputStreamWriter.write(body);
                outputStreamWriter.flush();// 刷新
            }
            if (httpURLConnection.getResponseCode() >= 300) {
                throw new Exception(
                        "HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
            }

            if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = httpURLConnection.getInputStream();
                outputStream = responce.getOutputStream();
                int bufferSize = 4 * 4 * 1024;// same buffer size as in Jetty utils  
                // (2*8192)  
                byte[] buffer = new byte[bufferSize];  
                int read;  
                while ((read = inputStream.read(buffer)) != -1) {  
                    outputStream.write(buffer, 0, read);  
                }  
            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {// 关闭流

            try {
                if (outputStreamWriter != null) {
                    outputStreamWriter.close();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return resultBuffer.toString();
    }

    /**
     * 将map集合的键值对转化成:key1=value1&key2=value2 的形式
     * 
     * @param parameterMap
     *            需要转化的键值对集合
     * @return 字符串
     */
    public static String convertStringParamter(Map parameterMap) {
        StringBuffer parameterBuffer = new StringBuffer();
        if (parameterMap != null) {
            Iterator iterator = parameterMap.keySet().iterator();
            String key = null;
            String value = null;
            while (iterator.hasNext()) {
                key = (String) iterator.next();
                if (parameterMap.get(key) != null) {
                    value = (String) parameterMap.get(key);
                } else {
                    value = "";
                }
                parameterBuffer.append(key).append("=").append(value);
                if (iterator.hasNext()) {
                    parameterBuffer.append("&");
                }
            }
        }
        return parameterBuffer.toString();
    }

}

控制器调用

    /**
     * 代理访问
     * @throws ServletException
     * @throws IOException
     */
    public void proxyHost2()  throws ServletException, IOException {
        String query = null;    
        query = request.getParameter("targetURL");    
        HttpConnectionUtil.requestMethod(HttpConnectionUtil.HTTP_GET, query, "",response);
    }

前台js调用代码

 var url = "http://服务器ip:6080/arcgis/services/axVector/MapServer/WFSServer?service=WFS&ersion=1.0.0&request=GetFeature&outputFormat=gml2&srsname=EPSG:4544&typename=axVetor:流转项目";
  url =  '/yzt/proxyHost2?targetURL=' + encodeURIComponent(url);  
                    $.ajax({  
                           type: "POST",  
                           url: url,  
                           dataType:"text",  
                           success: function(data){
                              var features = new ol.format.GML2().readFeatures(data);
                             //处理自己的逻辑
                           } 
                       });  

经过自己测试,代码都是可行的。

最后声明:以上代码均是很久前转载的,找不到原地址了,如果有看见的麻烦告知下。好加上原文连接。
ps: 想找到不用写代理类的方法,毕竟请求wfs带上filter的时候麻烦。

转载自:https://blog.csdn.net/mrjintao/article/details/78576884

You may also like...