#### 外部请求我们项目接口
- HttpClientUtil.java Http请求工具类
```
public static String post(String url, Map<String, String> paramMap) throws ClientProtocolException, IOException {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> formparams = setHttpParams(paramMap);
UrlEncodedFormEntity param = new UrlEncodedFormEntity(formparams, "UTF-8");
httpPost.setEntity(param);
HttpResponse response = httpClient.execute(httpPost);
String httpEntityContent = getHttpEntityContent(response);
httpPost.abort();
return httpEntityContent;
}
public static String get(String url) throws ClientProtocolException, IOException {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet();
httpGet.setURI(URI.create(url));
HttpResponse response = httpClient.execute(httpGet);
String httpEntityContent = getHttpEntityContent(response);
httpGet.abort();
return httpEntityContent;
}
```
> get 请求示例
```
try {
Map<String, String> map = new HashMap<>( );
String res = HttpClientUtil.get( "http://192.168.0.114:2222/products", map );
System.out.println( "res = " + res );
} catch (IOException e) {
e.printStackTrace();
}
```
- 运行返回成功
![](https://box.kancloud.cn/6b4fc995813fac3bfbb2b0e356f11149_1228x40.png)
> post 请求示例
```
try {
Map<String, String> map = new HashMap<>( );
// String res = HttpClientUtil.get( "http://192.168.0.114:2222/products", map );
// System.out.println( "res = " + res );
map.put( "Token", "mytoken" );
String post = HttpClientUtil.post( "http://192.168.0.114:9999/api-data/products", map );
System.out.println( "post = " + post );
} catch (Exception e) {
e.printStackTrace();
}
```
- 测试请求返回成功
![](https://box.kancloud.cn/62712a19335184d1624028d99c08a7d7_1264x72.png)