/**
* 发送post请求,参数用map接收
* @param url 地址
* @param map 参数
* @return 返回值
*/
public String postMap(String url,Map<String,String> map) {
//方法一
/* HttpPost httpPost = new HttpPost("http://localhost:8080/test/login.do");
try {
CloseableHttpClient client = HttpClients.createDefault();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "taozi"));
params.add(new BasicNameValuePair("password", "123"));
HttpEntity httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
httpPost.setEntity(httpEntity);
CloseableHttpResponse response = client.execute(httpPost);
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
httpPost.releaseConnection();
} */
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
//设置参数发送
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for(Map.Entry<String,String> entry : map.entrySet())
{
pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
CloseableHttpResponse response = null;
try {
post.setEntity(new UrlEncodedFormEntity(pairs,"UTF-8"));
response = httpClient.execute(post);
if(response != null && response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
if(response != null)
{
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
* 发送post请求,参数用map接收
* @param url 地址
* @param map 参数
* @return 返回值
*/
public String postMap(String url,Map<String,String> map) {
//方法一
/* HttpPost httpPost = new HttpPost("http://localhost:8080/test/login.do");
try {
CloseableHttpClient client = HttpClients.createDefault();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "taozi"));
params.add(new BasicNameValuePair("password", "123"));
HttpEntity httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
httpPost.setEntity(httpEntity);
CloseableHttpResponse response = client.execute(httpPost);
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
httpPost.releaseConnection();
} */
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
//设置参数发送
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for(Map.Entry<String,String> entry : map.entrySet())
{
pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
CloseableHttpResponse response = null;
try {
post.setEntity(new UrlEncodedFormEntity(pairs,"UTF-8"));
response = httpClient.execute(post);
if(response != null && response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
if(response != null)
{
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}