android使用apache httpclient发送post请求

package com.liuc;

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

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

public class HttpPostUtil {

	public static String sendHttpPost(Map<String, 	String> map,String encode,String path){

		List<NameValuePair> list=new ArrayList<NameValuePair>();
		if (map!=null&&!map.isEmpty()) {
			for(Map.Entry<String, String> entry:map.entrySet()){
				list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
		}
		try {
			//实现将参数封装到表单中,也就是请求体重
			UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list,encode);
			//使用post方式提交数据
			HttpPost httpPost=new HttpPost(path);
			httpPost.setEntity(entity);
			RequestConfig requestConfig = RequestConfig.custom().
					setSocketTimeout(2000).setConnectTimeout(2000).build();//4.3之后的设置请求和传输超时时间
			httpPost.setConfig(requestConfig);
			//执行post请求
			//3.X是这样的
			//HttpClient httpClient=new DefaultHttpClient();
			//4.x是这样的
			HttpClient httpClient = HttpClients.createDefault();
			HttpResponse httpResponse=httpClient.execute(httpPost);

			if (httpResponse.getStatusLine().getStatusCode()==200) {
				return getStringForInputStream(httpResponse.getEntity().getContent(),encode);
			}

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return path;

	}

	private static String getStringForInputStream(InputStream inputStream,
			String encode) {
		ByteArrayOutputStream stream=new ByteArrayOutputStream();
		byte[] data=new byte[1024];
		int len=0;
		String result="";
		try {
			if (inputStream != null) {
				while ((len = inputStream.read(data)) != -1) {
					stream.write(data, 0, len);
				}
				result=new String(stream.toByteArray(),encode);
			}
		} catch (Exception e) {
		}
		return result;
	}

}

对于各个版本写法的区别。可以参考:http://www.open-open.com/lib/view/open1383751765321.html

android使用apache httpclient发送post请求,布布扣,bubuko.com

时间: 2024-12-20 17:48:46

android使用apache httpclient发送post请求的相关文章

Android中使用HttpClient发送Get请求

这里要指定编码,不然服务器接收到的会是乱码的.

Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)

[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4006009.html 联系方式:[email protected] [系列]Android系列之网络:(持续更新) Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据) Android系列之网络(二)----HTTP请求头与响应头 Android

HTTPClient 发送HTTPS请求

HTTPClient 发送HTTP请求就不多说了, 现在给出发送HTTPS请求, 主要思路是忽略证书验证. /** * * @param url * @param contextType "image/jpeg","application/Json" * @return */ public static byte[] sendHttpsGetUrl(HttpClient httpClient1 ,String url,String contextType) { //

使用httpclient发送http请求

先来个httpclient的maven依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3</version> </dependency> SimpleHttpClient.java package com.openapi.TestPojo; im

Android面向HTTP协议发送get请求

/** * 採用get请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到的数据 */ public static String getRequest(String username, String password) { try { String path = "http://172.22.64.156:8080/0001AndroidWebService/LoginServlet?use

使用HttpClient发送GET请求

HttpRequestMessage http_req_msg = new HttpRequestMessage(); http_req_msg.Method = HttpMethod.Get; http_req_msg.Headers.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); http_req_msg.RequestUr

Android面向HTTP协议发送post请求

/** * 采用post请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到的数据 */ public static String postRequest(String username, String password) { try { String path = "http://172.22.64.156:8080/0001AndroidWebService/LoginServlet&q

Httpclient发送json请求

一.Httpclient发送json请求 public String RequestJsonPost(String url){    String strresponse = null;    try{        HttpClient hc = new DefaultHttpClient();       HttpPost hp = new HttpPost(url);       JSONObject jsonParam = new JSONObject();       jsonPara

.net core使用HttpClient发送代理请求_程序内抓包_Fiddler抓包

原文:.net core使用HttpClient发送代理请求_程序内抓包_Fiddler抓包 前言:  通过Fiddler抓取浏览器请求数据,相信大家已经都会用了,我们知道Fiddler是通过在本机计算器添加一个默认的代理服务器来实现的抓包数据的,端口号为:8888. 其实当我们打开Fiddler的设置也可以看到: 然后查看本地计算器的网络代理设置: 基于上面的原理,Fiddler就实现了经过本机计算器请求的数据抓包了... 那么,我们通过C#代码,在.net Core中使用HttpClient