华观科技
软件工匠,用心服务
HttpClient示例
更新日期: 2019年01月07日 14:24   来源: 本站   作者: 杨工   阅读: 972

httpClient示例代码

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;

import com.farseersoft.http.HttpClient;
import com.farseersoft.http.HttpClientException;
import com.farseersoft.http.HttpGet;
import com.farseersoft.http.HttpPost;
import com.farseersoft.http.HttpRequestEventListener;
import com.farseersoft.http.HttpResponse;
import com.farseersoft.http.entity.FileEntity;
import com.farseersoft.http.entity.FormEntity;
import com.farseersoft.http.entity.StringEntity;

/**
 * @Created by farseersoft.com
 * @author yangjincheng
 * @Date 2014年2月24日
 */
public class HttpClientTest {
	private static Log log = LogFactory.getLog(HttpClientTest.class);
	HttpClient httpclient = null;

	@Before
	public void setUp() throws Exception {
		httpclient = new HttpClient();
	}

	/**
	 * 模拟发送GET方式HTTP请求
	 */
	@Test
	public void testGet() {
		HttpGet get = new HttpGet("http://localhost:8080/fsr-mix/test/index.jsp");

		get.setEncoding("UTF-8");
		try {
			HttpResponse response = httpclient.execute(get);
			// System.out.println(response.getText());
			log.debug(response.getText());
		} catch (HttpClientException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 模拟提交表单
	 */
	@Test
	public void testPostForm() {
		HttpPost post = new HttpPost("http://localhost:8199/fsr-mix/test/httpclient/httpclient_action.jsp");
		post.setEncoding("UTF-8");

		FormEntity entity = new FormEntity();
		entity.addField("P1", "中文参数");
		entity.addField("P2", 2);
		entity.addField("strArray", new String[] { "AS", "BC" });
		entity.addField("intArray", new Integer[] { 12, 243, 345 });
		post.setEntity(entity);

		try {
			HttpResponse response = httpclient.execute(post);
			System.out.println(response.getText());
		} catch (HttpClientException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 模拟提交字符串数据
	 */
	@Test
	public void testPostString() {
		HttpPost post = new HttpPost("http://localhost:8080/ui/EagleActions/demoLoginAction");
		post.setEncoding("UTF-8");

		post.setEntity(new StringEntity("{\"action\":\"login\",\"arguments\":[\"sys\",\"123456\"],\"params\":{}}"));

		try {
			HttpResponse response = httpclient.execute(post);

			System.out.println(response.getText());
		} catch (HttpClientException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 模拟上传文件
	 */
	@Test
	public void testPostFile() {
		HttpPost post = new HttpPost("http://localhost:8080/ui/EagleActions/demoUploadFileAction");
		post.setEncoding("UTF-8");

		try {

			FileEntity entity = new FileEntity();

			entity.addFile(new File("docs/samplefile.txt"));
			entity.addFile(new File("docs/测试上传文件.txt"));
			entity.addFile(new File("K:/网页模板/欧美/201108028.rar"));

			entity.addField("action", "upload");

			post.setEntity(entity);

			httpclient.setRequestEventListener(new HttpRequestEventListener() {

				@Override
				public void progress(long total, long sended) {
					System.out.println(sended + "/" + total);
				}

				@Override
				public void completed() {
					System.out.println("上传完毕!");
				}
			});

			HttpResponse response = httpclient.execute(post);
			System.out.println(response.getText());
		} catch (HttpClientException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 模拟发送GET方式HTTP请求
	 */
	@Test
	public void testGetImage() {
		HttpGet get = new HttpGet(
				"http://125.35.6.80:8080/ftba/itownet/download.do?method=downloadFile&fid=1da860922d004074bb1a4536a13d5a64&ssid=aDdVeRhhcBy0K19IkSL5IA||");
		get.setEncoding("UTF-8");
		try {
			HttpResponse response = httpclient.execute(get);
			OutputStream output = new FileOutputStream("D:/test.jpg");
			response.write(output);
		} catch (HttpClientException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 模拟发送GET方式HTTP请求
	 */
	@Test
	public void testGetFSRWeb() {
		String[] columns = new String[] { "/", "/framework/", "/download/", "/case/", "/school/", "/recharge/" };

		for (int i = 0; i < 100; i++) {
			for (String c : columns) {
				HttpGet get = new HttpGet("http://localhost:8080/fsrweb/web" + c);
				get.setEncoding("UTF-8");
				try {
					HttpResponse response = new HttpClient().execute(get);
					response.getText();
				} catch (HttpClientException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			System.out.println("已执行" + i + "次");
		}

	}

}


软件工匠  用心服务
联系方式
邮箱:farseersoft@163.com
地址:广州市天河区东莞庄一横路116号广东生产力大厦11层1101-1104
关注公众号
扫码访问微站