当前位置: 代码迷 >> Web前端 >> httpclient 的有关应用
  详细解决方案

httpclient 的有关应用

热度:898   发布时间:2012-10-17 10:25:47.0
httpclient 的相关应用

?

1这个主要是 httpclient4.0.3 的一些常用操作

?

package com.woyo.abcp.mq.common;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public abstract class HttpClientHelper {

	public static String httpGet(String url) {

		// 消除没必要的日志
		System.setProperty("org.apache.commons.logging.Log",
				"org.apache.commons.logging.impl.SimpleLog");
		System.setProperty("org.apache.commons.logging.simplelog.showdatetime",
				"true");
		System
				.setProperty(
						"org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient",
						"stdout");

		// 构造HttpClient的实例
		HttpClient httpClient = new DefaultHttpClient();
		// httpClient.setTimeout(5000);
		// 创建GET方法的实例
		HttpGet getMethod = new HttpGet(url);
		// 使用系统提供的默认的恢复策略
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		try {
			// 执行getMethod
			return httpClient.execute(getMethod, responseHandler);
		} catch (IOException e) {
			// 发生网络异常
			e.printStackTrace();
		} finally {
			// 释放连接
			httpClient.getConnectionManager().shutdown();
		}
		return null;
	}

	public static String httpGet(String url, String charset) {

		// 消除没必要的日志
		System.setProperty("org.apache.commons.logging.Log",
				"org.apache.commons.logging.impl.SimpleLog");
		System.setProperty("org.apache.commons.logging.simplelog.showdatetime",
				"true");
		System
				.setProperty(
						"org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient",
						"stdout");

		// 构造HttpClient的实例
		HttpClient httpClient = new DefaultHttpClient();
		// httpClient.setTimeout(5000);
		// 创建GET方法的实例
		HttpGet getMethod = new HttpGet(url);
		// 使用系统提供的默认的恢复策略
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		try {
			return httpClient.execute(getMethod, responseHandler);
		} catch (IOException e) {
			// 发生网络异常
			e.printStackTrace();
		} finally {
			// 释放连接
			httpClient.getConnectionManager().shutdown();
		}
		return null;
	}

	public static String httpPost(String url, Map<String, String> params) {

		// 消除没必要的日志
		System.setProperty("org.apache.commons.logging.Log",
				"org.apache.commons.logging.impl.SimpleLog");
		System.setProperty("org.apache.commons.logging.simplelog.showdatetime",
				"true");
		System
				.setProperty(
						"org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient",
						"stdout");

		// 构造HttpClient的实例
		HttpClient httpClient = new DefaultHttpClient();
		// 创建Post方法的实例
		HttpPost postMethod = new HttpPost(url);
		// 将参数的值放入postMethod中
		if (params != null) {
			List<NameValuePair> values = new ArrayList<NameValuePair>();
			for (Map.Entry<String, ?> entity : params.entrySet()) {
				BasicNameValuePair pare = new BasicNameValuePair(entity
						.getKey(), entity.getValue().toString());
				values.add(pare);

			}

			UrlEncodedFormEntity entity;
			try {
				entity = new UrlEncodedFormEntity(values, "UTF-8");
				postMethod.setEntity(entity);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				System.out.println("httpclient  postmethod  参数赋值错误");
				e.printStackTrace();
			}

		}
		try {
			// 执行postMethod
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			return httpClient.execute(postMethod, responseHandler);
		} catch (IOException e) {
			// 发生网络异常
			e.printStackTrace();
		} finally {
			// 释放连接
			httpClient.getConnectionManager().shutdown();
		}
		return null;
	}

	/*
	 * public static void main(String[] args) throws Exception {
	 * 
	 * String url="http://cmt.wy/sapi/service_comments.php"; Map<String,String>
	 * params=new HashMap<String,String>(); params.put("method",
	 * "updateCommentsStatusForbid"); params.put("data",
	 * "{\"id\":\"123456\",\"target_id\":\"123456\",\"target_owner_id\":\"123456\",\"target_type\":\"12345\",\"channel_id\":\"123456\",\"channel_type\":\"123456\"}"
	 * ); String rs=httpPost(url,params); System.out.println(rs);
	 * 
	 * 
	 * // String jsonStr=HttpClientHelper.httpPost(url, params);
	 * 
	 * // System.out.println(jsonStr);
	 * 
	 * // Map <String,String> param=new HashMap<String,String>(); //
	 * param.put("user_id", "547064"); // param.put("album_id",
	 * "00706300000082"); // param.put("option", "3"); // param.put("reason",
	 * "0"); // param.put("checker_id", "988387"); //
	 * param.put("time","1295440915" ); // //
	 * 
	 * // String url = "http://photo.wy/sapi/check_album.php"; //
	 * HttpClientHelper
	 * .httpGet(url);//("http://photo.wy/sapi/check_album.php",param);
	 * Map<String, String> params = new HashMap<String, String>(); String url =
	 * "http://blog.wy/sapi/service_blog.php"; params.put("method",
	 * "admin_blog"); params.put("option", "1");// 通过 // params.put("user_id",
	 * "546064"); // params.put("blog_id", "007064000004170");
	 * 
	 * params.put("user_id", "636620"); params.put("blog_id", "00700630000399");
	 * 
	 * params.put("reason", "0");
	 * System.out.println(HttpClientHelper.httpPost(url, params));
	 * 
	 * }
	 */
}

?

  相关解决方案