package com.zushou365.postserver.utils;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;public class HttpHelper {
/**
     * 执行一个HTTP POST请求,返回请求响应的HTML
     *
     * @param url         请求的URL地址
     * @param params    请求的查询参数,可以为null
     * @param charset 字符集
     * @param pretty    是否美化
     * @return 返回请求响应的HTML
     */
    public static String doPost(String url, Map params, String charset, boolean pretty) {
        StringBuffer response = new StringBuffer();
        //构造HttpClient的实例
        HttpClient client = new HttpClient();
        client.getParams().setParameter("http.protocol.content-charset", charset); 
        //创建Post法的实例
        PostMethod method = new PostMethod(url);
        //使用系统提供的默认的恢复策略
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
        //设置Http Post数据
        if (params != null) {
            // 填入各个表单域的值
            NameValuePair[] data = null;
            
            Set sets = params.keySet();
            Object[] arr = sets.toArray();
            int mxsets = sets.size();
            if(mxsets>0){
                data=new NameValuePair[mxsets];
            }
            for (int i = 0; i < mxsets; i++) {
                String key = (String) arr[i];
                String val = (String) params.get(key);
                data[i]=new NameValuePair(key, val);
            }
            // 将表单的值放入postMethod中
            method.setRequestBody(data);
                        
        }
        try {
            //执行getMethod
            int statusCode =client.executeMethod(method);
            if (statusCode == HttpStatus.SC_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (pretty) {
                        response.append(line).append("\n");
                    } else {
                        response.append(line);
                    }
                }
                reader.close();
            }
        } catch(HttpException e){
            System.out.println("Http错误原因:" +e.getMessage());
        } catch (IOException e) {
            System.out.println("IO错误原因:" +e.getMessage());
        }
        finally {
            method.releaseConnection();
        }
        return response.toString();
    }    /**
     * 执行一个HTTP GET请求,返回请求响应的HTML
     *
     * @param url         请求的URL地址
     * @param params    请求的查询参数,可以为null
     * @param charset 字符集
     * @param pretty    是否美化
     * @return 返回请求响应的HTML
     */
    public static String doGet(String url, Map params, String charset, boolean pretty) {
        StringBuffer response = new StringBuffer();
        //构造HttpClient的实例
        HttpClient client = new HttpClient();
        client.getParams().setParameter("http.protocol.content-charset", charset); 
        //创建Get法的实例
        GetMethod method = new GetMethod(url);
        //使用系统提供的默认的恢复策略
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());        //设置Http Get数据
        if (params != null) {
            // 填入各个表单域的值
            NameValuePair[] data = null;            Set sets = params.keySet();
            Object[] arr = sets.toArray();
            int mxsets = sets.size();
            if(mxsets>0){
                data=new NameValuePair[mxsets];
            }
            for (int i = 0; i < mxsets; i++) {
                String key = (String) arr[i];
                String val = (String) params.get(key);
                data[i]=new NameValuePair(key, val);
            }
            // 将表单的值放入postMethod中
            method.setQueryString(data);
        }
        try {
            //执行getMethod
            int statusCode =client.executeMethod(method);
            if (statusCode == HttpStatus.SC_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (pretty) {
                        response.append(line).append("\n");
                    } else {
                        response.append(line);
                    }
                }
                reader.close();
            }
        } catch(HttpException e){
            System.out.println("Http错误原因:" +e.getMessage());
        } catch (IOException e) {
            System.out.println("IO错误原因:" +e.getMessage());
        }
        finally {
            method.releaseConnection();
        }
        return response.toString();
    }
    }
不确定原因,昨天晚上还能用,今天就不行了,一直调试到现在,找到问题的根本,因为不熟悉服务器所以不知道是不是服务器照成的,求指教!