package com.mmt.httpClient_GET;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientPost {
public static void main(String []args){
String urlPath = "http://localhost/test/userLogin.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userName", "mmt"));
params.add(new BasicNameValuePair("userPswd", "123456"));
String encodeType = "utf-8";
String resultString = doPost(urlPath,params,encodeType);
System.out.println(resultString);
}
private static String doPost(String urlPath,List<NameValuePair> params,String encodeType){
String resultString = null ;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(urlPath);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
resultString = EntityUtils.toString(httpEntity);
} catch (Exception e) {
e.printStackTrace();
}
return resultString ;
}
}