HttpUtils(2)
|
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.HttpURLConnection; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.HashMap; /** * * <p>Title: </p> * <p>Description: http utils </p> * <p>Company: </p> * @version 1.0 */ public class HttpUtils { private static final String URL_PARAM_CONNECT_FLAG = "&"; private static Log log = LogFactory.getLog(HttpUtils.class); private HttpUtils() { } /** * GET METHOD * @param strUrl String * @param map Map * @throws IOException * @return List */ public static List URLGet(String strUrl, Map map) throws IOException { String strtTotalURL = ""; List result = new ArrayList(); if(strtTotalURL.indexOf("?") == -1) { strtTotalURL = strUrl + "?" + getUrl(map); } else { strtTotalURL = strUrl + "&" + getUrl(map); } log.debug("strtTotalURL:" + strtTotalURL); URL url = new URL(strtTotalURL); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setUseCaches(false); con.setFollowRedirects(true); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); while (true) { String line = in.readLine(); if (line == null) { break; } else { result.add(line); } } in.close(); return (result); } /** * POST METHOD * @param strUrl String * @param content Map * @throws IOException * @return List */ public static List URLPost(String strUrl, Map map) throws IOException { String content = ""; content = getUrl(map); String totalURL = null; if(strUrl.indexOf("?") == -1) { totalURL = strUrl + "?" + content; } else { totalURL = strUrl + "&" + content; } URL url = new URL(strUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setAllowUserInteraction(false); con.setUseCaches(false); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=GBK"); BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(con. getOutputStream())); bout.write(content); bout.flush(); bout.close(); BufferedReader bin = new BufferedReader(new InputStreamReader(con. getInputStream())); List result = new ArrayList(); while (true) { String line = bin.readLine(); if (line == null) { break; } else { result.add(line); } } return (result); } /** * ���URL * @param map Map * @return String */ private static String getUrl(Map map) { if (null == map || map.keySet().size() == 0) { return (""); } StringBuffer url = new StringBuffer(); Set keys = map.keySet(); for (Iterator i = keys.iterator(); i.hasNext(); ) { String key = String.valueOf(i.next()); if (map.containsKey(key)) { Object val = map.get(key); String str = val!=null?val.toString():""; try { str = URLEncoder.encode(str, "GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } url.append(key).append("=").append(str). append(URL_PARAM_CONNECT_FLAG); } } String strURL = ""; strURL = url.toString(); if (URL_PARAM_CONNECT_FLAG.equals("" + strURL.charAt(strURL.length() - 1))) { strURL = strURL.substring(0, strURL.length() - 1); } return (strURL); } } |
HttpUtils(2)的更多相关文章
- XUtils框架中HttpUtils使用Get请求时总是返回相同信息的问题解决,xutilshttputils
如需转载请标明出处:http://blog.csdn.net/itas109 版本:Xutils 2014年11月11日 下载地址:https://github.com/wyouflf/xUtils ...
- 使用httputils上传图片到服务器
//创建httpUtils对象 HttpUtils mRegHttpUtils = new HttpUtils(); //图片路径 String path = "/sdcard/Downlo ...
- 3. Android框架和工具之 xUtils(HttpUtils)
1. HttpUtils 作用: 支持同步,异步方式的请求: 支持大文件上传,上传大文件不会oom: 支持GET,POST,PUT,MOVE,COPY,DELETE,HEAD请求: 下载支持301/3 ...
- 反射机制及开源框架xUitls的使用,使用HttpUtils通过断点续传下载文件
反射: Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法: 对于任意一个对象,都能够调用它的任意一个方法和属性 Java反射机制主要提供下面几种用途: 1•在运行时判断 ...
- xutils的HttpUtils,Post和Get基本使用,以及BitmapUtils的简单使用
开篇报错注意:本教程是基于xUtils-2.6.14.jar版本实现的 由于studio中6.0以后安卓取消了httpclient,而xutils则基于httpclient开发的,所以现在无法使用,将 ...
- XUtils骨架HttpUtils采用Get总是返回请求解决问题的相同信息
如需转载请注明出处:http://blog.csdn.net/itas109 版本号:Xutils 2014年11月11日 下载地址:https://github.com/wyouflf/xUtils ...
- Androids含文档erver结束(工具包 Httputils)两
在同server在...的基础上,本文client还登录界面 Andriod简单http get请求基础上,用户注冊后跳转到下载界面,本文下载界面仅仅有两个View,一个是textView显示注冊后u ...
- 客户端 HttpUtils.java
package com.http.post; import java.io.ByteArrayOutputStream; import java.io.IOException; import java ...
- 使用HttpUtils 上传视频文件
private void shangchuan(){ //文件的路径 //File file=new File(path); File fi ...
- 使用HttpUtils完成Http Basic 认证
调用声网(agora)的远程接口(Restful Api)时,对方需要使用Basic Auth的方式进行认证(需要输入用户名和密码). 一,使用Postman完成基于Basic Auth的Http认证 ...
随机推荐
- 进程间通信之数据传输--FIFO
One of the fundamental features that makes Linux and other Unices useful is the “pipe”. Pipes allow ...
- ps aux|grep *** 解释
对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程, 而ps命令(Process Status)就是最基本同时也是非常强大的进程查看命令. 使用该命令 可以确定有哪些进程正在运 ...
- nginx: [error] invalid PID number "" in "/run/nginx.pid"
在重启云主机(系统)之后,执行 nginx -t 是OK的,然而在执行 nginx -s reload 的时候报错 nginx: [error] invalid PID number “” in “/ ...
- 《TensorFlow2深度学习》学习笔记(三)Tensorflow进阶
本篇笔记包含张量的合并与分割,范数统计,张量填充,限幅等操作. 1.合并与分割 合并 张量的合并可以使用拼接(Concatenate)和堆叠(Stack)操作实现,拼接并不会产生新的维度,而堆叠会创建 ...
- Codeforces C. Elections(贪心枚举三分)
题目描述: C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 《基于WEB的独立学院补考重修管理系统研究》论文笔记(二十)
<基于WEB的独立学院补考重修管理系统研究>论文笔记(1) 一.基本信息 标题:基于WEB的独立学院补考重修管理系统研究 时间:2016 来源:南通大学杏林学院 关键词:WEB:补考重修管 ...
- Linux学习笔记——管道PIPE
管道:当从一个进程连接数据流到另一个进程时,使用术语管道(pipe).# include <unistd.h> int pipe(int filedes[2]); //创建管道 pipe( ...
- javaWeb开发中entityBean的习惯用法
entity bean的作用是将与数据库交互的过程封装成对象,在servelet编写的过程中,只需要会写java,不必考虑与数据库交互细节. 实体类: 基本与数据库的表相对应,表示一个实在的对象. 例 ...
- Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)
链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...
- Greenplum 6 新功能 在线扩容工具GPExpand (转载)
Gpexpand是Greenplum数据库的扩容工具,可以为集群增加新节点从而可以存储更多的数据,提供更高的计算能力.Greenplum 5及之前,集群扩容需要停机增加新节点,然后对表数据做重分布.因 ...