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", "[email protected]"));
			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();
			}
	  }
}

 

 

你可能感兴趣的:(HttpClient4 POST数据及问题)