package location.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.entity.StringEntity;
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;
/**
* description: 使用 HttpClient 实现服务端跨域请求
* @version v1.0
* @author w
* @date 2018年9月10日下午3:20:32
*/
public class HttpClientUtils {
/**
* description: 使用httpClient对象执行get请求
* @param uri 需要跨域请求的uri
* @return
* @throws ClientProtocolException
* @throws IOException
* String
* @version v1.0
* @author w
* @date 2018年9月10日 下午3:27:49
*/
public static String doGet(String uri) throws ClientProtocolException, IOException{
// 1、 创建 httpClient 对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 2、 创建 get 对象
HttpGet get =new HttpGet(uri);
// 3、 执行 get 请求
CloseableHttpResponse response = httpClient.execute(get);
// 4、 获取返回结果 HttpEntity 对象
HttpEntity entity = response.getEntity();
// 5、获取返回结果中的数据
String data = EntityUtils.toString(entity);
// 6、 关闭 response、 关闭 httpClient
response.close();
httpClient.close();
return data;
}
/**
* description: 使用httpClient对象执行 post 请求
* @param uri 需要跨域请求的uri , formDataMap 模拟表单需要提交数据 (name - value 形式)
* @param formDataMap
* @return
* @throws ClientProtocolException
* @throws IOException
* String
* @version v1.0
* @author w
* @date 2018年9月10日 下午3:42:10
*/
public static String doPost(String uri,Map<String,Object> formDataMap) throws ClientProtocolException, IOException{
// 1、创建httpClient 对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 2、 创建post 对象
HttpPost post =new HttpPost(uri);
// 3、 创建一个list形式数据,模拟提交表单。
List<NameValuePair> formDataList=new ArrayList<>();
// TODO: 这里可以遍历模拟表单传递过来的数据 formDataMap
/*Iterator<Entry<String, Object>> iterator = formDataMap.entrySet().iterator();
while(iterator.hasNext()){
Entry<String, Object> next = iterator.next();
String key = next.getKey();
String value = next.getValue().toString();
formDataList.add(new BasicNameValuePair(key, value));
}*/
formDataList.add(new BasicNameValuePair("ids", "110"));
formDataList.add(new BasicNameValuePair("name", "httpClient 请求数据"));
// 4、 把表单数据包装到entity 对象中 (StringEntity)
StringEntity formData = new UrlEncodedFormEntity(formDataList, "UTF-8");
post.setEntity(formData);
// 5、 执行post请求
CloseableHttpResponse response = httpClient.execute(post);
// 6、 获取响应数据
HttpEntity entity = response.getEntity();
// 7、 响应数据转换为字符串
String data = EntityUtils.toString(entity);
// 8、 关闭 httpClient对象、关闭 response
response.close();
httpClient.close();
return data;
}
public static void main(String[] args) throws ClientProtocolException, IOException {
long start = System.currentTimeMillis();
String url = "http://pv.sohu.com/cityjson?ie=utf-8";
String doGet = doGet(url);
System.out.println(doGet);
System.out.println((System.currentTimeMillis()-start)+ " ms");
}
}