httpclient 小例子编写

 

最近这两天帮忙调试接口,用到了httpclient ,自己参照网上方法编写个小例子 ,方便以后查看

-----

 

/**
 * 远程访问调用方法工具类
 * 
 * @author yangy
 * 
 */
public class RemoteRequestUtils
{

	public static void main(String[] args) throws HttpException, IOException
	{
		getRequest(null);
	}

	/**
	 * 远程get请求 方法直接打印返回结果
	 * 
	 * @param url
	 * @param parmete
	 * @throws IOException
	 * @throws HttpException
	 * @throws Exception
	 */
	public static void getRequest(String url) throws HttpException, IOException
	{
		// 创建请求对象
		HttpClient client = new HttpClient();
		client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
		//设置超时时间
		client.getParams().setSoTimeout(2000);

		// 创建远程访问
		GetMethod method = new GetMethod(
				url);

		// 设置成了默认的恢复策略,在发生异常时候将自动重试3次,在这里你也可以设置成自定义的恢复策略
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());

		// 执行getMethod
		int statusCode = client.executeMethod(method);

		if (statusCode != HttpStatus.SC_OK)
			{
				System.err.println("Method failed: " + method.getStatusLine());
			}
		
		//获取数据
		String responseBody = readInputStream(method.getResponseBodyAsStream());
		System.out.println(" Remoter request success :" + responseBody);

		// 释放连接
		method.releaseConnection();

	}

	/**
	 * 远程post请求 方法直接打印返回结果
	 * 
	 * @param url
	 * @param parmete
	 * @throws IOException
	 * @throws HttpException
	 */
	public static void postRequest(String url, NameValuePair[] data)
			throws HttpException, IOException
	{
		// 创建请求对象
		HttpClient httpClient = new HttpClient();
		// 设置超时时间
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
				20000);

		// 创建POST对象
		UTF8PostMethod postMethod = new UTF8PostMethod(
				url);

		// 设置成了默认的恢复策略,在发生异常时候将自动重试3次,在这里你也可以设置成自定义的恢复策略
		postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());
		
		// 将表单的值放入postMethod中
		postMethod.setRequestBody(data);

		// 执行postMethod
		int statusCode = httpClient.executeMethod(postMethod);
		// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发

		System.out.println(statusCode);
		String location = "";

		// 返回返回结果
		if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
				|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
			{
				// 从头中取出转向的地址
				Header locationHeader = postMethod.getResponseHeader("location");
	
				if (locationHeader != null)
					{
						location = locationHeader.getValue();
						System.out.println("The page was redirected to:" + location);
					} 
				else
					{
						System.err.println("Location field value is null.");
					}
			} 
		else if (statusCode == HttpStatus.SC_OK) // 返回为连接成功
			{
				// 此处修改为返回为输入流 避免大数量时出错 消耗内存
				String responseBody = readInputStream(postMethod
						.getResponseBodyAsStream());
				System.out.println(" Remoter request success :" + responseBody);
			}

		// 释放连接
		postMethod.releaseConnection();

	}

	/**
	 * 转换Inputstram为字符串
	 * 
	 * @param responseBody
	 * @return * @throws IOException
	 */
	private static String readInputStream(InputStream responseBody)
			throws IOException
	{
		//创建字符串数读取缓存
		BufferedReader buffre = new BufferedReader(new InputStreamReader(
				responseBody,"utf-8")); //使用utf-8 避免出现乱码
		//创建缓存字符串
		StringBuffer resBuffer = new StringBuffer(10000);
		String resTemp = "";
		while ((resTemp = buffre.readLine()) != null)
			{
				resBuffer.append(resTemp).append("\n"); //读取字符流中数据 目前测试可能存在换行添加
			}
		return resBuffer.toString();
	}

}

目前代码能够执行成功, 蛋痛的是 调用对方接口 每次一调用他们的系统就挂掉...

 

转载于:https://my.oschina.net/luckyi/blog/69188

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值