Java tutorial
/** * PureInfo Force * @(#)HttpUtilImplTest.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 java.io.UnsupportedEncodingException; import junit.framework.TestCase; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.log4j.Logger; import com.pureinfo.force.io.exception.IOTransferException; /** * <P> * Created on Dec 26, 2005 10:16:13 AM <BR> * Last modified on Dec 26, 2005 * </P> * * @author Freeman.Hu * @version 1.0, Dec 26, 2005 * @since Force 1.0 */ public class HttpUtilImplTest extends TestCase { private static final Logger logger = Logger.getLogger(HttpUtilImplTest.class); private HttpUtilImpl m_util = null; protected void setUp() throws Exception { m_util = new HttpUtilImpl(); } public void testGetContent() throws IOTransferException { if (logger.isDebugEnabled()) { logger.debug("testGetContent() - start"); logger.debug("Googlehtml"); } String content = m_util.getContent("http://www.google.com", null, "utf-8"); System.out.println(content); if (logger.isDebugEnabled()) { logger.debug("testGetContent() - end"); } } public void testGetContentWithArgs() throws IOTransferException { if (logger.isDebugEnabled()) { logger.debug("testGetContentWithArgs() - start"); logger.debug("HTML"); } NameValuePair[] args = new NameValuePair[] { new NameValuePair("Email", "Freemanhu"), new NameValuePair("Passwd", "huxiaowen"), new NameValuePair("PersistentCookie", "no") }; String content = m_util.getContent("https://www.google.com/accounts/LoginAuth", args, "utf-8"); System.out.println(content); if (logger.isDebugEnabled()) { logger.debug("testGetContentWithArgs() - end"); } } public void testConnectErrorURL() { if (logger.isDebugEnabled()) { logger.debug("testConnectErrorURL() - start"); logger.debug(""); } try { m_util.getContent("http://199.155.122.100:1234/srm-center", null, "utf-8"); fail("can't reach here!"); } catch (IOTransferException ex) { logger.error("testConnectErrorURL()", ex); assertTrue(true); } if (logger.isDebugEnabled()) { logger.debug("testConnectErrorURL() - end"); } } public void testConnectByWrongProxy() { m_util = new HttpUtilImpl("proxy.zju.edu.cn", 6666, "user", "password"); if (logger.isDebugEnabled()) { logger.debug("testGetContent() - start"); logger.debug("Googlehtml"); } try { m_util.getContent("http://www.google.com", null, "utf-8"); fail("can't reach here"); } catch (IOTransferException ex) { logger.error("", ex); } } public void testConnectByRightProxy() throws IOTransferException { if (logger.isDebugEnabled()) { logger.debug("testConnectByRightProxy() - start"); logger.debug(",808test/test"); logger.debug("google"); } m_util = new HttpUtilImpl("localhost", 808, "test", "test"); String content = m_util.getContent("http://www.google.com", null, "utf-8"); System.out.println(content); if (logger.isDebugEnabled()) { logger.debug("testGetContent() - end"); } if (logger.isDebugEnabled()) { logger.debug("testConnectByRightProxy() - end"); } } public void testRetryCount() { if (logger.isDebugEnabled()) { logger.debug("testRetryCount() - start"); logger.debug(""); } m_util.setRetryCount(4); try { m_util.getContent("http://199.155.122.100:1234/srm-center", null, "utf-8"); fail("can't reach here!"); } catch (IOTransferException ex) { logger.error("testRetryCount()", ex); assertTrue(true); } if (logger.isDebugEnabled()) { logger.debug("testRetryCount() - end"); } } public void testConnect() throws IOTransferException, UnsupportedEncodingException { HttpUtilImpl util = new HttpUtilImpl(); String content = util.getContent("http://srm.pureinfo.com.cn/srm/LoginEntry.do", null, "utf-8"); System.out.println(content); } public void testResponseCharset() throws Exception { final String sUrl = "http://srm.pureinfo.com.cn/srm/Login.do"; //final String sUrl = // "http://199.155.122.100:9521/srm-center/data-sync.do"; HttpClient client = new HttpClient(); PostMethod method = new PostMethod(sUrl); method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); try { client.executeMethod(method); System.out.println("charset=" + method.getResponseCharSet()); String sContent = method.getResponseBodyAsString(); System.out.println("content=" + sContent); } finally { method.releaseConnection(); } } }