喵星之旅-狂奔的兔子-使用java实现http请求(无需登录)
一、获取jar
实现任何功能肯定是先获取相应工具类,即jar包。可以使用复制的方式或者maven获取。项目中已导入jar,可以直接使用。
需要的jar入图:
项目地址:svn://47.105.188.20/kitty/2%E3%80%81code/httpdemo 用户名密码:reader/reader
maven地址:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.32</version> </dependency>
二、编写工具类
package club.kittybunny.httpclient; import java.util.HashMap; import java.util.Map; import org.apache.http.HttpResponse; 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.util.EntityUtils; import com.alibaba.fastjson.JSON; /** * 除了使用项目提供的jar也可以使用maven下载,参考地址如下 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.32</version> </dependency> * @author bunny~~我是兔子我会喵,我叫喵星兔。 * */ public class HttpUtil { /** 请求编码 */ private static final String DEFAULT_CHARSET = "UTF-8"; /** * 执行HTTP POST请求 * * @param url * url * @param param * 参数 * @return */ public static String httpPostWithJSON(String url, Map<String, ?> param) { CloseableHttpClient client = null; try { if (url == null || url.trim().length() == 0) { throw new Exception("URL is null"); } HttpPost httpPost = new HttpPost(url); client = HttpClients.createDefault(); if (param != null) { StringEntity entity = new StringEntity(JSON.toJSONString(param), DEFAULT_CHARSET);// 解决中文乱码问题 entity.setContentEncoding(DEFAULT_CHARSET); entity.setContentType("application/json"); httpPost.setEntity(entity); } HttpResponse resp = client.execute(httpPost); if (resp.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(resp.getEntity(), DEFAULT_CHARSET); } } catch (Exception e) { e.printStackTrace(); } finally { close(client); } return null; } /** * 执行HTTP GET请求 * * @param url * url * @param param * 参数 * @return */ public static String httpGetWithJSON(String url, Map<String, ?> param) { CloseableHttpClient client = null; try { if (url == null || url.trim().length() == 0) { throw new Exception("URL is null"); } client = HttpClients.createDefault(); if (param != null) { StringBuffer sb = new StringBuffer("?"); for (String key : param.keySet()) { sb.append(key).append("=").append(param.get(key)).append("&"); } url = url.concat(sb.toString()); url = url.substring(0, url.length() - 1); } HttpGet httpGet = new HttpGet(url); HttpResponse resp = client.execute(httpGet); if (resp.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(resp.getEntity(), DEFAULT_CHARSET); } } catch (Exception e) { e.printStackTrace(); } finally { close(client); } return null; } /** * 关闭HTTP请求 * * @param client */ private static void close(CloseableHttpClient client) { if (client == null) { return; } try { client.close(); } catch (Exception e) { } } }
三、编写测试类
public static void main(String[] args) { Map<String, String> param = new HashMap<>(); param.put("name", "hehe"); String result = httpGetWithJSON("http://127.0.0.1:9999/hello", param); System.out.println("result:" + result); }
执行结果如下:
作者:喵星兔
出处:https://www.cnblogs.com/kittybunny/
喵星之旅:https://www.cnblogs.com/kittybunny/p/12148641.html
我的视频:https://space.bilibili.com/518581788
更多内容:不咬人的小兔子
本博客所有文章仅用于学习、研究和交流目的,欢迎非商业性质转载。
我是兔子,我会喵,我叫喵星兔~~