com.pureinfo.force.net.impl.HttpUtilImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.force.net.impl.HttpUtilImpl.java

Source

/**
 * PureInfo Force
 * @(#)HttpUtilImpl.java   1.0 Dec 26, 2005
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.force.net.impl;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.ProxyHost;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.pureinfo.force.io.exception.IOTransferException;
import com.pureinfo.force.net.IHttpUtil;

/**
 * <P>
 * Created on Dec 26, 2005 9:50:39 AM <BR>
 * Last modified on Dec 26, 2005
 * </P>
 * The utilities for the http protocol.
 * 
 * @author Freeman.Hu
 * @version 1.0, Dec 26, 2005
 * @since Force 1.0
 */
public final class HttpUtilImpl implements IHttpUtil {
    private ProxyHost m_proxyHost;

    private String m_sProxyUser;

    private String m_sProxyPassword;

    private int m_nRetryCount = 0;

    public HttpUtilImpl() {
    }

    public HttpUtilImpl(ProxyHost _host, String _sUser, String _sPassword) {
        m_proxyHost = _host;
        m_sProxyUser = _sUser;
        m_sProxyPassword = _sPassword;
    }

    public HttpUtilImpl(String _sProxyHost, int _nProxyPort, String _sUser, String _sPassword) {
        m_proxyHost = new ProxyHost(_sProxyHost, _nProxyPort);
        m_sProxyUser = _sUser;
        m_sProxyPassword = _sPassword;
    }

    public ProxyHost getProxyHost() {
        return m_proxyHost;
    }

    public void setProxyHost(ProxyHost _nProxyHost) {
        m_proxyHost = _nProxyHost;
    }

    public String getProxyPassword() {
        return m_sProxyPassword;
    }

    public void setProxyPassword(String _sPassword) {
        m_sProxyPassword = _sPassword;
    }

    public String getProxyUser() {
        return m_sProxyUser;
    }

    public void setProxyUser(String _sUser) {
        m_sProxyUser = _sUser;
    }

    public int getRetryCount() {
        return m_nRetryCount;
    }

    public void setRetryCount(int _nRetryCount) {
        m_nRetryCount = _nRetryCount;
    }

    /**
     * @see com.pureinfo.force.net.IHttpUtil#getContent(String, NameValuePair[],
     *      String)
     */
    public String getContent(String _sUrl, NameValuePair[] _args, String _sRequestCharset)
            throws IOTransferException {
        return getContent(_sUrl, _args, _sRequestCharset, null);
    }

    /**
     * @see com.pureinfo.force.net.IHttpUtil#getContent(String, NameValuePair[],
     *      String, String)
     */
    public String getContent(String _sUrl, NameValuePair[] _args, String _sRequestCharset, String _sResponseCharset)
            throws IOTransferException {
        if (_sRequestCharset == null) {
            _sRequestCharset = "utf-8";
        }

        //1. to create http client and set proxy
        HttpClient client = new HttpClient();
        if (m_proxyHost != null) {
            client.getHostConfiguration().setProxyHost(m_proxyHost);
            if (m_sProxyUser != null & m_sProxyPassword != null) {
                client.getState().setProxyCredentials(//
                        new AuthScope(m_proxyHost.getHostName(), m_proxyHost.getPort()),
                        new UsernamePasswordCredentials(m_sProxyUser, m_sProxyPassword));
            }
        }

        //2. to create post
        PostMethod method = new PostMethod(_sUrl);
        if (m_nRetryCount > 0) {
            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, //
                    new DefaultHttpMethodRetryHandler(m_nRetryCount, false));
        }
        method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=" + _sRequestCharset);
        for (int i = 0; _args != null && i < _args.length; i++) {
            method.addParameter(_args[i]);
        }

        //3. to send request and read response
        String sResponse;
        try {
            client.executeMethod(method);
            sResponse = method.getResponseBodyAsString();
            if (!method.getRequestCharSet().equals(_sRequestCharset)) {
                sResponse = new String(sResponse.getBytes(), _sResponseCharset);
            }
            return sResponse;
        } catch (Exception ex) {
            throw new IOTransferException(_sUrl, ex);
        } finally {
            method.releaseConnection();
        }
    }
}