[Java语言] Java网络请求工具类
2016-04-19 17:53
716 查看
Java网络请求工具类(依赖:org.apache.http;注:HttpClient 4.4,HttpCore 4.4) 到此处可以去下载依赖包:http://hc.apache.org/downloads.cgi import java.util.List; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; 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.util.EntityUtils; http://www.nvzi91.cn/LEEP/30054.html /** * HttpServletUtil * * @author ysj * @Date: 2015-1-30 下午2:07:55 */ public class HttpServletUtil { private static CloseableHttpClient httpclient; private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build(); /** * Post:访问数据库并返回数据字符串 *http://www.nvzi91.cn/kunmingrenliu/041930055.html * @param params * 向服务器端传的参数 * @param url * @return String 数据字符串 * @throws Exception */ public static String doPost(List<NameValuePair> params, String url) throws Exception { String result = null; httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); //设置请求和传输超时时间 httpPost.setConfig(requestConfig); CloseableHttpResponse httpResp = httpclient.execute(httpPost); try { int statusCode = httpResp.getStatusLine().getStatusCode(); // 判断是够请求成功 if (statusCode == HttpStatus.SC_OK) { System.out.println("状态码:" + statusCode); System.out.println("请求成功!"); // 获取返回的数据 result = EntityUtils.toString(httpResp.getEntity(), "UTF-8"); } else { System.out.println("状态码:" + httpResp.getStatusLine().getStatusCode()); System.out.println("HttpPost方式请求失败!"); } } finally { httpResp.close(); httpclient.close(); }http://www.nvzi91.cn/shengzhizhengxing/30056.html return result; } /** * Get:访问数据库并返回数据字符串 * * @param url * @return String 数据字符串 * @throws Exception */ public static String doGet(String url) throws Exception{ String result = null; httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); //设置请求和传输超时时间 httpGet.setConfig(requestConfig); CloseableHttpResponse httpResp = httpclient.execute(httpGet); try { int statusCode = httpResp.getStatusLine().getStatusCode(); // 判断是够请求成功 if (statusCode == HttpStatus.SC_OK) { System.out.println("状态码:" + statusCode); System.out.println("请求成功!"); // 获取返回的数据 result = EntityUtils.toString(httpResp.getEntity(), "UTF-8"); } else {http://www.nvzi91.cn/luanchaonanzhong/30057.html System.out.println("状态码:" + httpResp.getStatusLine().getStatusCode()); System.out.println("HttpGet方式请求失败!"); }http://www.nvzi91.cn/fujianyan/30058.html } finally { httpResp.close(); httpclient.close(); }www.nvzi91.cn return result; } } |
相关文章推荐
- HTTP协议详解(真的很经典)
- $_SERVER['HTTP_USER_AGENT']
- TCP SYN泛洪攻击
- 介绍HTTPS
- BZOJ4538 HNOI2016 网络
- 计算机网络学习笔记
- org.jasig.cas.client.session.SingleSignOutHttpSessionListener
- [网络流24题] 最长递增子序列 (最多不相交路径---网络最大流)
- Http返回码
- 移动网络策略
- $GLOBALS ["HTTP_RAW_POST_DATA"] 取不到值如何排除
- Http
- 循序渐进Java Socket网络编程(多客户端、信息共享、文件传输)
- 【网虫】windows下网管员必备的几条网络相关命令
- HttpStatus返回状态码
- 在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决
- 循环神经网络(RNN, Recurrent Neural Networks)介绍
- 深度学习卷积神经网络大事件一览
- java jsoup 网络爬虫 学习例子(二) 只抓取豆瓣电影5星(力荐)电影名称
- apache http server/tomcat 端口问题