`

HttpClient4使用(转)

 
阅读更多

详见:http://www.oschina.net/code/snippet_54371_1515

 

 

需用到的包:

commons-logging-1.1.1.jar

httpclient-4.0.3.jar

httpcore-4.0.1.jar

 

 

package main;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class Main {

	private static HttpClient hc = new DefaultHttpClient();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email", "xxx@gmail.com"));
		params.add(new BasicNameValuePair("pwd", "xxx"));
		params.add(new BasicNameValuePair("save_login", "1"));

		String url = "http://www.oschina.net/action/user/login";

		String body = post(url, params);
		System.out.println(body);
	}

	/**
	 * Get请求
	 * @param url
	 * @param params
	 * @return
	 */
	public static String get(String url, List<NameValuePair> params) {
		String body = null;
		try {
			// Get请求
			HttpGet httpget = new HttpGet(url);
			// 设置参数
			String str = EntityUtils.toString(new UrlEncodedFormEntity(params));
			httpget.setURI(new URI(httpget.getURI().toString() + "?" + str));
			// 发送请求
			HttpResponse httpresponse = hc.execute(httpget);
			// 获取返回数据
			HttpEntity entity = httpresponse.getEntity();
			body = EntityUtils.toString(entity);
			if (entity != null) {
				entity.consumeContent();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return body;
	}

	/**
	 * // Post请求
	 * @param url
	 * @param params
	 * @return
	 */
	public static String post(String url, List<NameValuePair> params) {
		String body = null;
		try {
			// Post请求
			HttpPost httppost = new HttpPost(url);
			// 设置参数
			httppost.setEntity(new UrlEncodedFormEntity(params));
			// 发送请求
			HttpResponse httpresponse = hc.execute(httppost);
			// 获取返回数据
			HttpEntity entity = httpresponse.getEntity();
			body = EntityUtils.toString(entity);
			if (entity != null) {
				entity.consumeContent();
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}

}
 

 

分享到:
评论

相关推荐

    httpclient

    以下列出的是 HttpClient 提供的主要的功能,要知道更多详细的功能可以参见 HttpClient 的主页。 (1)实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等) (2)支持自动转向 (3)支持 HTTPS 协议 (4)支持代理...

    字符串文件http密码资源文件openoffice图片等工具等

    Pinyin4jUtil中文转拼音、中文转拼音首字母、中文转字节序列 PwdUtils生成4-32位的随机密码 StringEncoding字母串转码 StringUtil字符串关于数字中文的判断与转换方法 UrlCoderUtil字符串url编码解码

    httpcomponents-client-4.2.1-bin.zip

    HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。本文首先介绍 ...(4)支持代理服务器等

    Java转换Json需要的Jar包集合

    3.commons-httpclient-3.0.jar 4.commons-lang-2.4.jar 5.commons-logging-1.0.4.jar 6.ezmorph-1.0.6.jar 7.json-lib-2.3-jdk15.jar 对于没有积分的童鞋,我已经把所需要的jar包已经贴出来了,自己动手,丰衣足食.

    Spark-Streaming+HDFS实战

    - 使用Spark Streaming处理数据集,可以使用httpclient获取数据。 - 对获取的数据进行必要的转换、清洗和处理,以满足需求。 - 将处理后的数据保存到HDFS文件系统。 ## 4. HDFS文件保存: - 使用Spark ...

    精通ANDROID 3(中文版)1/2

    11.1.8 使用AndroidHttpClient  11.1.9 使用后台线程(AsyncTask)  11.1.10 使用AsyncTask处理配置更改  11.1.11 使用DownloadManager获取文件  11.2 使用Android服务  11.2.1 Android中的服务  11.2.2...

    精通Android 3 (中文版)2/2

    11.1.8 使用AndroidHttpClient  11.1.9 使用后台线程(AsyncTask)  11.1.10 使用AsyncTask处理配置更改  11.1.11 使用DownloadManager获取文件  11.2 使用Android服务  11.2.1 Android中的服务  11.2.2...

    java开源包4

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    wechat:微信玩1.2.7

    wechat-微信play1.2.x模块 基于wechat4j开发 使用方式 将conf/wechat4j.properties拷贝到主项目的conf下面,并配置...如果您在使用过程中有什么问题,请直接发邮件到zhangpdg#gmail.com(#转@) 参考例子: 感谢wechat4j

    疯狂Android讲义源码

     13.3.2 使用Apache HttpClient 501  13.4 使用WebView视图  显示网页 505  13.4.1 使用WebView浏览网页 506  13.4.2 使用WebView加载HTML  代码 507  13.5 使用Web Service进行  网络编程 508  13.5.1 Web...

    疯狂Android讲义.part1

    13.3.2 使用Apache HttpClient 501 13.4 使用WebView视图显示 网页 506 13.4.1 使用WebView浏览网页 506 13.4.2 使用WebView加载HTML 代码 507 13.5 使用Web Service进行网络 编程 508 13.5.1 Web Service简介 509 ...

    Unirest轻量级的HTTP开发库 v3.14.2

    4.支持路径参数 5.支持gzip 6.支持本地的 Basic Authentication 7.自定义超时,并发级别和代理设置。 8.为每个请求自定义默认请求头。 9.自定义HttpClient和HttpAsyncClient 10.自动解析JSON 11.自定义的将...

    Android实例代码

    第4章、深入理解Activity 4.1、建立、配置和使用Activity: 4.2、Activity的回调机制: 4.3、Activity的生命周期: 第5章、使用Intent和IntentFilter进行通信 5.1、Intent对象详解: 5.2、Intent的属性及intent...

    springboot调用第三方查询天气.zip

    具体业务逻辑 ...3.请求 一般使用httpclient,springboot通过restTemplate 来请求 4.获取请求的body 5.通过ObjectMapper把请求的body转成ResponseResult 6.objectMapper.readValue(待转数据,哪个类.class);

    疯狂Android讲义(第2版)源代码 第6章~第9章

    第4章、深入理解Activity 4.1、建立、配置和使用Activity: 4.2、Activity的回调机制: 4.3、Activity的生命周期: 第5章、使用Intent和IntentFilter进行通信 5.1、Intent对象详解: 5.2、Intent的属性及intent...

    angular_fastfood

    功能性HttpClient 处理使用SpringBoot发出的HTTP请求。 身份验证和验证管理Angular Interceptor和JSON Web令牌| 角度验证。 动画管理角度动画| 在每个页面上进行的淡入/淡出转换。 授权管理角卫兵。屏幕截图演示版...

Global site tag (gtag.js) - Google Analytics