Fork me on GitHub

Java中模拟HTTP请求

1.基于HttpURLConnection
2.基于HttpClient (httpclient4.2.3 httpcore4.2.2 HttpClient支持异步,需要Jar包)

image

public class HttpUtil {
    private final static Logger logger = LogManager.getLogger(HttpUtil.class);

    /**
     * 支持普通HTTP请求 WCF WEBSERVICE
     * 
     * @param url
     * @param xml
     *            WCF XML参数
     * @param method
     * @param contentType
     * @param timeOut
     * @return
     */
    public static String HttpProxy(String url, String xml, String method, String contentType, String timeOut) {
        OutputStream os = null;
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        StringBuffer sb = null;
        try {
            if (StringUtils.isEmpty(method)) {
                method = "GET";
            }
            if (StringUtils.isEmpty(contentType)) {
                contentType = "application/x-www-form-urlencoded";// "application/x-java-serialized-object"
            }
            if (StringUtils.isEmpty(timeOut)) {
                timeOut = "5000";
            }
            URL _url = new URL(url);
            conn = (HttpURLConnection) _url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod(method);
            conn.setRequestProperty("Content-type", contentType);
            conn.setConnectTimeout(Integer.parseInt(timeOut));
            conn.setRequestProperty("Content-type", contentType);
            // conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            // 请求参数
            if (!StringUtils.isEmpty(xml)) {
                os = conn.getOutputStream();
                os.write(xml.getBytes());
                os.flush();
            }
            // 返回值
            if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {
                conn.connect();
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                sb = new StringBuffer();
                String str = null;
                while ((str = reader.readLine()) != null) {
                    sb.append(str + "\n");
                }
            }
            return sb.toString();
        } catch (IOException ex) {
            logger.info("url:" + url + " exception:" + ex.getMessage());
        } finally {
            try {
                if (conn != null) {
                    conn.disconnect();
                }
                if (reader != null) {
                    reader.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * HttpClient Get请求
     * 
     * @param url
     * @param encoding
     * @return
     */
    public static String HttpGetProxy(String url, String encoding) {
        HttpClient client = new DefaultHttpClient();
        // 设置为get取连接的方式.
        HttpGet get = new HttpGet(url);
        HttpResponse response;
        String str = null;
        StringBuffer sb = new StringBuffer();
        BufferedReader reader = null;
        if (StringUtils.isEmpty(encoding)) {
            encoding = "UTF-8";
        }
        try {
            response = client.execute(get);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // 得到返回的主体内容.
                reader = new BufferedReader(new InputStreamReader(entity.getContent(), encoding));
                while ((str = reader.readLine()) != null) {
                    sb.append(str + "\n");
                }
            }
            return sb.toString();
        } catch (Exception ex) {
            logger.info("url:" + url + " exception:" + ex.getMessage());
        } finally {
            try {
                client.getConnectionManager().shutdown();
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * HttpClient Post请求  带参数
     * 
     * @param url
     * @param encoding
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String HttpPostProxy(String url, Map params, String encoding) {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        BufferedReader reader = null;
        StringBuffer sb = new StringBuffer();
        if (StringUtils.isEmpty(encoding)) {
            encoding = "UTF-8";
        }
        // 请求参数
        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        if (params != null && params.keySet().size() > 0) {
            Iterator iterator = params.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Entry) iterator.next();
                postParams.add(new BasicNameValuePair((String) entry.getKey(), (String) entry.getValue()));
            }
        }
        HttpPost httpost = new HttpPost(url);
        httpost.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");// "application/x-www-form-urlencoded"
        httpost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0");
        httpost.setHeader("Accept", "text/html, */*");
        httpost.setEntity(new UrlEncodedFormEntity(postParams, Consts.UTF_8));
        try {
            try {
                response = httpclient.execute(httpost);
                HttpEntity entity = response.getEntity();
                System.out.println("Login form get: " + response.getStatusLine());
                if (entity != null) {
                    reader = new BufferedReader(new InputStreamReader(entity.getContent(), encoding));
                    String str = "";
                    while ((str = reader.readLine()) != null) {
                        sb.append(str + "\n");
                    }
                }
                // 处理Cookies
                List<Cookie> cookies = httpclient.getCookieStore().getCookies();
                if (cookies.isEmpty()) {
                    System.out.println("None");
                } else {
                    for (int i = 0; i < cookies.size(); i++) {
                        System.out.println("Cookie: - " + cookies.get(i).toString());
                    }
                }
                return sb.toString();
            } catch (IOException ex) {
                logger.info("url:" + url + " exception:" + ex.getMessage());
            }
        } finally {
            try {
                httpclient.getConnectionManager().shutdown();
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

Just Test

@Test
    public void TestHttp() {
        // getInput();
        // getUrl("http://cnblogs.com/", "utf-8");

        // System.out.println(HttpUtil.HttpProxy("http://cnblogs.com/", "", "", "", "1000"));
        // System.out.println(HttpUtil.HttpGetProxy("http://cnblogs.com/", ""));

        // WCF
        // String url = "http://api.homeinns.com/CWAP_beta2/webs/GetHotelNmByCityCD.aspx";
        // StringBuffer param_xml = new StringBuffer(500);
        // param_xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("<CwapMessage xmlns=\"http://wap.homeinns.com/cwap\">").append(
        // "<version>1.0</version>").append("<TxtMsgMessage>").append("<Lisence>800332</Lisence>").append("<CityCD>0210</CityCD>").append(
        // "</TxtMsgMessage>").append("</CwapMessage>");
        // System.out.println(HttpUtil.HttpProxy(url, param_xml.toString(), null, null, null));

        Map<String, String> params = new HashMap<String, String>();
        params.put("beginDate", "2013-04-21");
        params.put("cityName", "上海");
        params.put("del_session_flag", "false");
        params.put("endDate", "2013-04-22");
        params.put("pn", "1");
        params.put("range", "50000");
        params.put("sCityCd", "0210");
        params.put("promotion", "");
        params.put("sHotelBrand", "");
        params.put("sLandMark", "");
        params.put("sLandMarkName", "");
        params.put("sRegion", "");
        params.put("searchFlag", "");
        params.put("sortField", "");
        params.put("strlatlon", "");
        params.put("subwayId", "");
        System.out.println(HttpUtil.HttpPostProxy("http://www.homeinns.com/loadHotel.html", params, ""));
    }

image

posted @ 2013-04-21 16:22  花儿笑弯了腰  阅读(768)  评论(0编辑  收藏  举报