CSDNRGY 2015-11-12 01:38 采纳率: 88.2%
浏览 3375

关于HttpClient的问题,如何使用HttpClient重定向?

 /*
 * HttpRequestProxy.java
 *
 * Created on November 3, 2008, 9:53 AM
 */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;


public class HttpRequestProxy {
    // 超时间隔
    private static int connectTimeOut = 60000;
    // 让connectionmanager管理httpclientconnection时是否关闭连接
    private static boolean alwaysClose = false;
    // 返回数据编码格式
    private String encoding = "UTF-8";

    private final HttpClient client = new HttpClient();

    public HttpClient getHttpClient() {
        return client;
    }
    /**
     * 用法: HttpRequestProxy hrp = new HttpRequestProxy();
     * hrp.doRequest("http://www.163.com",null,null,"gbk");
     * 
     * @param url
     *            请求的资源URL
     * @param postData
     *            POST请求时form表单封装的数据 没有时传null
     * @param header
     *            request请求时附带的头信息(header) 没有时传null
     * @param encoding
     *            response返回的信息编码格式 没有时传null
     * @return response返回的文本数据
     * @throws CustomException
     */
    public String doRequest(String url, Map postData, Map header, String encoding) throws Exception {
        String responseString = null;
        // 头部请求信息
        Header[] headers = null;
        if (header != null) {
            Set entrySet = header.entrySet();
            int dataLength = entrySet.size();
            headers = new Header[dataLength];
            int i = 0;
            for (Iterator itor = entrySet.iterator(); itor.hasNext();) {
                Map.Entry entry = (Map.Entry) itor.next();
                headers[i++] = new Header(entry.getKey().toString(), entry.getValue().toString());
            }
        }

        // post方式
        if (postData != null) {
            PostMethod postRequest = new PostMethod(url.trim());
            if (headers != null) {
                for (int i = 0; i < headers.length; i++) {
                    postRequest.setRequestHeader(headers[i]);
                }
            }
            Set entrySet = postData.entrySet();
            int dataLength = entrySet.size();
            NameValuePair[] params = new NameValuePair[dataLength];
            int i = 0;
            for (Iterator itor = entrySet.iterator(); itor.hasNext();) {
                Map.Entry entry = (Map.Entry) itor.next();
                params[i++] = new NameValuePair(entry.getKey().toString(), entry.getValue().toString());
            }
            postRequest.setRequestBody(params);
            try {
                responseString = this.executeMethod(postRequest, encoding);
            } catch (Exception e) {
                throw e;
            } finally {
                postRequest.releaseConnection();
            }
        }
        return responseString;
    }

    private String executeMethod(HttpMethod request, String encoding) throws Exception {
        String responseContent = null;
        InputStream responseStream = null;
        BufferedReader rd = null;
        try {
            this.getHttpClient().executeMethod(request);
            if (encoding != null) {
                responseStream = request.getResponseBodyAsStream();
                rd = new BufferedReader(new InputStreamReader(responseStream, encoding));
                String tempLine = rd.readLine();
                StringBuffer tempStr = new StringBuffer();
                String crlf = System.getProperty("line.separator");
                while (tempLine != null) {
                    tempStr.append(tempLine);
                    tempStr.append(crlf);
                    tempLine = rd.readLine();
                }
                responseContent = tempStr.toString();
            } else
                responseContent = request.getResponseBodyAsString();

            Header locationHeader = request.getResponseHeader("location");
            // 返回代码为302,301时,表示页面己经重定向,则重新请求location的url,这在
            // 一些登录授权取cookie时很重要
            if (locationHeader != null) {
                String redirectUrl = locationHeader.getValue();
                this.doRequest(redirectUrl, null, null, null);
            }
        } catch (HttpException e) {
            throw new Exception(e.getMessage());
        } catch (IOException e) {
            throw new Exception(e.getMessage());

        } finally {
            if (rd != null)
                try {
                    rd.close();
                } catch (IOException e) {
                    throw new Exception(e.getMessage());
                }
            if (responseStream != null)
                try {
                    responseStream.close();
                } catch (IOException e) {
                    throw new Exception(e.getMessage());

                }
        }
        return responseContent;
    }

    /**
     * 特殊请求数据,这样的请求往往会出现redirect本身而出现递归死循环重定向 所以单独写成一个请求方法
     * 比如现在请求的url为:http://localhost:8080/demo/index.jsp 返回代码为302
     * 头部信息中location值为:http://localhost:8083/demo/index.jsp
     * 这时httpclient认为进入递归死循环重定向,抛出CircularRedirectException异常
     * 
     * @param url
     * @return
     * @throws CustomException
     */
    public String doSpecialRequest(String url, int count, String encoding) throws Exception {
        String str = null;
        InputStream responseStream = null;
        BufferedReader rd = null;
        GetMethod getRequest = new GetMethod(url);
        // 关闭httpclient自动重定向动能
        getRequest.setFollowRedirects(false);
        try {

            this.client.executeMethod(getRequest);
            Header header = getRequest.getResponseHeader("location");
            if (header != null) {
                // 请求重定向后的URL,count同时加1
                this.doSpecialRequest(header.getValue(), count + 1, encoding);
            }
            // 这里用count作为标志位,当count为0时才返回请求的URL文本,
            // 这样就可以忽略所有的递归重定向时返回文本流操作,提高性能
            if (count == 0) {
                getRequest = new GetMethod(url);
                getRequest.setFollowRedirects(false);
                this.client.executeMethod(getRequest);
                responseStream = getRequest.getResponseBodyAsStream();
                rd = new BufferedReader(new InputStreamReader(responseStream, encoding));
                String tempLine = rd.readLine();
                StringBuffer tempStr = new StringBuffer();
                String crlf = System.getProperty("line.separator");
                while (tempLine != null) {
                    tempStr.append(tempLine);
                    tempStr.append(crlf);
                    tempLine = rd.readLine();
                }
                str = tempStr.toString();
            }

        } catch (HttpException e) {
            throw new Exception(e.getMessage());
        } catch (IOException e) {
            throw new Exception(e.getMessage());
        } finally {
            getRequest.releaseConnection();
            if (rd != null)
                try {
                    rd.close();
                } catch (IOException e) {
                    throw new Exception(e.getMessage());
                }
            if (responseStream != null)
                try {
                    responseStream.close();
                } catch (IOException e) {
                    throw new Exception(e.getMessage());
                }
        }
        return str;
    }

    public static void main(String[] args) throws Exception {

        HttpRequestProxy hrp = new HttpRequestProxy();
        Map date = new HashMap();
        date.put("jyidApplet", "1");
        date.put("codeNumApplet", "1");
        date.put("jymxIdApplet", "447");
        date.put("patientIdApplet", "1118");
        String str = hrp.doRequest("http://127.0.0.1:8080/lis/mz/addTM",date, null, null);
        System.out.println(str);

    }

}

  • 写回答

1条回答 默认 最新

  • CSDNRGY 2015-11-12 01:45
    关注

    executeMethod()方法中的Header locationHeader = request.getResponseHeader("location");
    我把location填写成了我要重定向的地址 http://192.168.1.122:8080/lis/mz/Mzquerypaging.action,但是不好使,哪位大神用过这个东西

    评论

报告相同问题?

悬赏问题

  • ¥15 静电纺丝煅烧后如何得到柔性纤维
  • ¥15 (标签-react native|关键词-镜像源)
  • ¥100 照片生成3D人脸视频
  • ¥15 伪装视频时长问题修改MP4的时长问题,
  • ¥15 JETSON NANO
  • ¥15 VS开发qt时如何在paintgl函数中用pushbutton控制切换纹理
  • ¥20 关于 openpyxl 处理excel文件地问题
  • ¥15 MS中不知道高分子的构型怎么构建模型
  • ¥60 QQOP数据,什么是op数据号,怎么提取op数据!能不能大量提取(语言-c语言)
  • ¥15 matlab代码 关于微分方程和嵌套的分段函数。