package com.learn.http;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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
public static void main(String[] args)
new HttpSend().post("http://www.baidu.com");
new HttpSend().get("http://www.baidu.com");
* 发送 post请求访问本地应用并根据传递参数不同返回不同结果
*/
public void post(String url)
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
UrlEncodedFormEntity uefEntity;
try
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
System.out.println("executing post request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
try
HttpEntity entity = response.getEntity();
if (entity != null)
System.out.println("--------------------------------------");
System.out.println("Response content: " + EntityUtils.toString(entity));
System.out.println("--------------------------------------");
finally
response.close();
catch (ClientProtocolException e)
e.printStackTrace();
catch (UnsupportedEncodingException e1)
e1.printStackTrace();
catch (IOException e)
e.printStackTrace();
finally
try
httpclient.close();
catch (IOException e)
e.printStackTrace();
* 发送 get请求
*/
public void get(String url)
CloseableHttpClient httpclient = HttpClients.createDefault();
try
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("cityEname", "henan"));
String str = EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));
System.out.println(str);
HttpGet httpGet = new HttpGet(url+"?"+str);
HttpGet httpget = new HttpGet(url);
System.out.println("executing get request " + httpget.getURI());
CloseableHttpResponse response = httpclient.execute(httpget);
try
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null)
System.out.println("Response content length: " + entity.getContentLength());
System.out.println("Response content: " + EntityUtils.toString(entity));
System.out.println("------------------------------------");
finally
response.close();
catch (ClientProtocolException e)
e.printStackTrace();
catch (ParseException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
finally
try
httpclient.close();
catch (IOException e)
e.printStackTrace();