`
sw1982
  • 浏览: 503868 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

HttpClient4 POST数据及问题

 
阅读更多

post 方式挂参数的三种格式, mark一下。 

 

其中尤其需要注意的是下面这个error()的调用方法,使用到MultipartEntity 带3个参数的完整 , 会导致请求参数在服务器端无法获取到post参数! 

  

MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT, null, charsetObj);

 

but上面这个new MultipartEntity,如果使用无参的构造函数则一切ok, public MultipartEntity() 

 

--------------

具体细节待深究

 

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
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.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


public class Test {
	
	final static String URL = "http://localhost/";
	final static Charset charsetObj = Charset.forName("utf-8");

	public static void main(String[] args) throws Exception {
		error();
//		demo() ;
//		method3();
	}
	
	  public static void demo() throws Exception {  
		     DefaultHttpClient httpclient = new DefaultHttpClient();  
		    // 目标地址  
		     HttpPost httppost = new HttpPost(URL);  
		    // 构造最简单的字符串数据  
		     StringEntity reqEntity = new StringEntity("firstname=abcde&lastname=fghi");  
		    // 设置类型  
		     reqEntity.setContentType("application/x-www-form-urlencoded");  
		    // 设置请求的数据  
		     httppost.setEntity(reqEntity);  
		    // 执行  
				HttpResponse httpresponse = httpclient.execute(httppost);
				HttpEntity entity = httpresponse.getEntity();
				String body = EntityUtils.toString(entity); 
				System.out.println(body);

	  }

	  public static void error() {
			int tryTimes = 3;
			MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT, null, charsetObj);
			try {
				HttpClient client = new DefaultHttpClient();
//				client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3000);
				HttpPost httppost = new HttpPost(URL);

				multipartEntity.addPart("firstname", new StringBody("xxxxxxxxxxxxxxxxxxxxxxx",charsetObj));
				multipartEntity.addPart("lastname", new StringBody("yyyyyyyyyyyyyyyyyyyyyyy",charsetObj));
				//设置连接超时时间
				//设置读取response超时时间
				httppost.setEntity(multipartEntity);
				
				HttpResponse httpresponse = client.execute(httppost);
				HttpEntity entity = httpresponse.getEntity();
				String body = EntityUtils.toString(entity);
				System.out.println(body);	 
			}catch (Exception e) {
				System.out.println(e);
			}
	  }
	  
	  public static void method3() {
			HttpClient client = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(URL);

			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")); 

			try {
				// Post请求
				// 设置参数
				httppost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
				// 发送请求
				HttpResponse httpresponse = client.execute(httppost);
				HttpEntity entity = httpresponse.getEntity();
				String body = EntityUtils.toString(entity);
				System.out.println(body);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	  }
}

 

 

分享到:
评论
4 楼 chjy1983 2014-09-25  
请教下,我这样:
JSONObject jsonObject = new JSONObject();//入参
jsonObject.put("acc_nbr", "13335659988");
jsonObject.put("latn_id", "551");
jsonObject.put("month_id", "201408");
StringEntity se = new StringEntity(jsonObject.toString(), "utf-8");
httpPost.setEntity(se);
HttpResponse response = httpclient.execute(httpPost);//发出请求

struts2 action中怎么获取post jsonObject的参数??
3 楼 tmq3244286 2013-06-26  
请问demo()中构造简单的字符串参数时怎样才可以不设置参数名直接传值
类似StringEntity reqEntity = new StringEntity("strValue");这样
2 楼 zhangxiaoboaq 2012-07-07  
服务端获取不到post参数
1 楼 zhangxiaoboaq 2012-07-07  
我这怎么运行不成功呢,给我解释一下

相关推荐

Global site tag (gtag.js) - Google Analytics