nginx rewrite(301)重定向之后无法正常请求的解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lupengfei1009/article/details/76857088

前言

前面通过腾讯云ssl证书实现了https的请求,如果想简单了解可参考如何添加SSL证书实现https请求;为了满足之前http请求不受影响,在nginx上面不仅配置了https的443端口的监听,同时监听了80端口。通过浏览器测试确实是没有问题,但是在写代码时,java代码发起get,post请求的时候,出现了“301 Moved Permanently”的错误;网上也没有搜索到好的解决方案,经过一下午的分析,找到了一个突破口,但是总觉得并不是一个好的方式,再此分享一下,如果有人能有更好的方式解决此问题,烦请告知一下。

问题追踪

  • 获取HeaderFields
    当我们通过一个http的地址得到一个URLConnection连接之后;通过getHeaderFields()获取这个连接的响应头,代码如下:

        String urlName = url + "?" + param;
        URL realUrl = new URL(urlName);
        // 打开和URL之间的连接
        URLConnection conn = realUrl.openConnection();
        Map<String, List<String>> map = conn.getHeaderFields();
        // 遍历所有的响应头字段
        for (String key : map.keySet())
        {
            System.out.println(key + "--->" + map.get(key).get(0));
        }
    • 如果是http的请求我们可以看到如下信息:

      这里给出了新的重定向后的地址

    • 如果是https的请求,就不会出现这个Location字段;并且能正常请求成功,效果如下:

    • 浏览器能正常请求个人认为是在出现错误之后,浏览器发现存在新地址Location字段;就取到了新地址重新发起了一次新的请求从而实现了正常的响应。

  • 解决方案
    既然上面说道浏览器检测到Location字段,拿到新的地址重新发起了一次新的请求,那么代码也同样是可以这么实现的,因此调整了代码,写了如下工具类:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.List;
    import java.util.Locale;
    import java.util.Map;
    
    import org.apache.commons.httpclient.util.HttpURLConnection;
    import org.apache.http.ParseException;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    public class HttpClientUtil
    {
    
        /**
         * 向指定URL发送GET方法的请求
         * 
         * @param url
         *            发送请求的URL
         * @param param
         *            请求参数,请求参数应该是name1=value1&name2=value2的形式。
         * @return URL所代表远程资源的响应
         */
    
        public static String sendGet(String url, String param)
        {
            String result = "";
            BufferedReader in = null;
            try
            {
                String urlName = url + "?" + param;
                URL realUrl = new URL(urlName);
                // 打开和URL之间的连接
                URLConnection conn = realUrl.openConnection();
    
                String newUrl = urlName;
                Map<String, List<String>> map = conn.getHeaderFields();
                // 遍历所有的响应头字段
                for (String key : map.keySet())
                {
                    //如果发现有重定向了新的地址
                    if ("Location".equals(key))
                    {
                        //获取新地址
                        newUrl = map.get(key).get(0) + "?" + param;
                        break;
                    }
                }
                // 重新实例化url对象
                realUrl = new URL(newUrl);
                // 重新打开和URL之间的连接
                conn = realUrl.openConnection();
    
                // 设置通用的请求属性
                conn.setRequestProperty("accept", "*/*");
                conn.setRequestProperty("connection", "Keep-Alive");
                conn.setRequestProperty("Accept-Charset", "UTF-8");
                conn.setRequestProperty("contentType", "UTF-8");
                conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
                conn.setRequestProperty("Accept-Language", Locale.getDefault().toString());
                // 建立实际的连接
                conn.connect();
                // 定义BufferedReader输入流来读取URL的响应
                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                String line;
                while ((line = in.readLine()) != null)
                {
                    result += line;
                }
            }
            catch (Exception e)
            {
                System.out.println("发送GET请求出现异常!" + e);
                e.printStackTrace();
            }
            // 使用finally块来关闭输入流
            finally
            {
                try
                {
                    if (in != null)
                    {
                        in.close();
                    }
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }
            return result;
        }
    
    
        /**
         * post请求
         */
        public static String sendPost(String url, String param)
        {
            PrintWriter out = null;
            BufferedReader in = null;
            String result = "";
            try
            {
                URL realUrl = new URL(url);
                // 打开和URL之间的连 ?
                URLConnection conn = realUrl.openConnection();
                Map<String, List<String>> map = conn.getHeaderFields();
                // 遍历所有的响应头字段
                String newUrl = url;
                for (String key : map.keySet())
                {
                    //如果发现有重定向了新的地址
                    if ("Location".equals(key))
                    {
                        //获取新地址
                        newUrl = map.get(key).get(0);
                        break;
                    }
                }
                realUrl = new URL(newUrl);
                // 打开和URL之间的连接
                conn = realUrl.openConnection();
    
                // 设置通用的请求属 ?
                conn.setRequestProperty("accept", "*/*");
                conn.setRequestProperty("connection", "Keep-Alive");
                conn.setRequestProperty("Accept-Charset", "UTF-8");
                conn.setRequestProperty("contentType", "UTF-8");
                conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
                conn.setRequestProperty("Accept-Language", Locale.getDefault().toString());
                // 发起POST请求必须设置如下两行
                conn.setDoOutput(true);
                conn.setDoInput(true);
                // 获取URLConnection对象对应的输出流
                out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
                // 发 ?请求参数
                out.print(param);
                // flush输出流的缓冲
                out.flush();
                // 定义BufferedReader输入流来读取URL的响应
                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                String line;
                while ((line = in.readLine()) != null)
                {
                    result += line;
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            // 使用finally块来关闭输出流 ?输入 ?
            finally
            {
                try
                {
                    if (out != null)
                    {
                        out.close();
                    }
                    if (in != null)
                    {
                        in.close();
                    }
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }
            return result;
        }
    }

总结

虽说通过调整客户端请求的时候通过调整,实现重定向的功能,但是我认为这并不是最好的解决方式;个人是希望能通过nginx那块的配置实现http到https的重定向,从而不必要修改客户端请求时的代码即可完成这块的动作,如果有那位大牛知道怎么做,烦请留言告知,万分感谢!

猜你喜欢

转载自blog.csdn.net/lupengfei1009/article/details/76857088
今日推荐