网络请求HttpUrlConnection的GET/PSOT请求框架
来源:互联网 发布:图片格式转换软件 编辑:程序博客网 时间:2024/06/05 13:34
写在前面 :本篇博客单单属于请求网络数据的工具类,可直接粘贴使用public static String postUrlConnect(String urlPath, Map<String, Object> map) { StringBuffer sbRequest = new StringBuffer(); if (map != null && map.size() > 0) { for (String key : map.keySet()) { sbRequest.append(key + "=" + map.get(key) + "&"); } } String request = sbRequest.substring(0, sbRequest.length() - 1); try { URL url = new URL(urlPath); HttpURLConnection httpurl = (HttpURLConnection) url.openConnection(); httpurl.setRequestMethod("POST"); //设置连接主机超时 httpurl.setConnectTimeout(30000); //设置从主机读取数据超时 httpurl.setReadTimeout(30000); httpurl.setDoInput(true); httpurl.setDoOutput(true); //读取数据 OutputStream os = httpurl.getOutputStream(); os.write(request.getBytes()); //刷新 os.flush(); //判断返回值是否正确 if (httpurl.getResponseCode() == 200) { InputStream in = httpurl.getInputStream(); StringBuffer sb = new StringBuffer(); //创建字符数组 byte[] buff = new byte[1024]; int len = -1; while ((len = in.read(buff)) != -1) { sb.append(new String(buff, 0, len, "utf-8")); } //关闭流 in.close(); os.close(); httpurl.disconnect(); return sb.toString(); } else { return null; } } catch (Exception e) { e.printStackTrace(); } return urlPath; } public static String getUrlConnect(String urlPath) { try { //获取地址 URL url = new URL(urlPath); HttpURLConnection httpurl = (HttpURLConnection) url.openConnection(); httpurl.connect(); //判断返回值 if (httpurl.getResponseCode() == 200) { InputStream in = httpurl.getInputStream(); StringBuffer sb = new StringBuffer(); byte[] buff = new byte[1024]; int len = -1; while ((len = in.read(buff)) != -1) { sb.append(new String(buff, 0, len, "utf-8")); } in.close(); httpurl.disconnect(); return sb.toString(); } else { return null; } } catch (Exception e) { e.printStackTrace(); } return null; }
阅读全文
0 0
- 网络请求HttpUrlConnection的GET/PSOT请求框架
- HTTP请求:get与psot的区别
- HttpURLConnection 的get请求
- HttpURLConnection的get请求
- HttpURLConnection的get请求
- HttpUrlConnection Get请求网络数据
- HttpURLConnection,get,post,网络请求
- HttpURLConnection 网络请求 get post
- HttpURLConnection的get请求总结
- HttpUrlConnection的get请求方式
- Android网络请求,Get方式请求,HttpURLConnection请求工具类
- HttpUrlConnection的GET请求和POST请求
- httpurlconnection的Get请求和Post请求
- HttpURLConnection--HttpURLConnection的Get请求方式
- 网络请求----HttpURLConnection的get,post和图片加载
- httpURLConnection-网络请求的两种方式-get请求和post请求
- httpURLConnection-网络请求的两种方式-get请求和post请求
- HttpURLConnection:POST和GET 网络请求
- Java爬虫系列之二网页解析【爬取知乎首页信息】
- meta 元标签的常用用法
- 自定义服务(四)命令实现的脚本
- 往本地路径写数据
- 转的 侵立删 java版本迭代
- 网络请求HttpUrlConnection的GET/PSOT请求框架
- 学习Oracle数据库(2)-SQLPLUS介绍
- dubbo+zookeeper管理控制台实践
- SourceInsight4.0的使用
- plsql developer启动缓慢的根本原因!
- 查看远程Redis服务器的版本
- docker上安装ubuntu
- Android零基础入门第43节:ListView优化和列表首尾使用
- 1068 Bash游戏 V3