直接代码:
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import java.util.Map; /** * http 工具类 */ public class HttpUtil { /* * * 调用: * HttpUtil.post(talkUrl, accessToken,"application/json", param); */ public static String post(String requestUrl, String accessToken, String params) throws Exception { String contentType = "application/x-www-form-urlencoded"; return HttpUtil.post(requestUrl, accessToken, contentType, params); } public static String post(String requestUrl, String accessToken, String contentType, String params) throws Exception { String encoding = "UTF-8"; if (requestUrl.contains("nlp")) { encoding = "GBK"; } return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding); } public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding) throws Exception { String url = requestUrl + "?access_token=" + accessToken; return HttpUtil.postGeneralUrl(url, contentType, params, encoding); } public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding) throws Exception { URL url = new URL(generalUrl); // 打开和URL之间的连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); // 设置通用的请求属性 connection.setRequestProperty("Content-Type", contentType); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setUseCaches(false); connection.setDoOutput(true); connection.setDoInput(true); // 得到请求的输出流对象 DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(params.getBytes(encoding)); out.flush(); out.close(); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map<String, List<String>> headers = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : headers.keySet()) { System.err.println(key + "--->" + headers.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 BufferedReader in = null; in = new BufferedReader( new InputStreamReader(connection.getInputStream(), encoding)); String result = ""; String getLine; while ((getLine = in.readLine()) != null) { result += getLine; } in.close(); System.err.println("result:" + result); return result; } }
...
相关推荐
请求http工具 支持ssl,请求参数支持json数据和map数据,能自动转换编码),不必担心返回数据乱码
http请求工具类 用于发送HTTP请求 发送后得到相应结果 并进行解析处理
网络请求工具类
非常齐全的http工具类,可以发送/接收各种请求。包括文件的发送,获取,带token验证的请求等
Http工具类HttpUtil.java:这个文件是工作用到的工具类,上传上来以后自己用到或者有同学也需要。 maven项目添加如下的jar包依赖,文件放到项目中修改一下package就可以直接使用了。 <groupId>org.apache....
HttpClient远程调用工具类 get 和post 调用用访问别的系统接口 用http方式请求,包括调用时用的工具类
基于Apache HttpClient 4.5.2 封装的工具类 HttpUtil 可用于爬虫和模拟登陆。使用新的语法后比3.*版本的速度感觉有提升。使用后注意资源的释放 pom 部分,应该不全,需要用的根据代码的import去找maven资源即可。 ...
Java,通过 httpclient 获取 cookie 模拟登录 ,登录后携带cookie发起下一个请求
本文实例为大家分享了Android网络连接工具类HttpUtil的使用方法,供大家参考,具体内容如下 该工具实现了发送get和post请求,请求的结果以String字符串的形式返回,比较适合接收服务器端发送过来的JSON字符串数据 ...
用于HTTP请求的工具类,支持GET,POST,HTTP,HTTPS 代码简单易懂
http连接请求,可以支持请求get,post方式请求表单,支持上传文件
Base64 工具类:Base64Util 文件读取工具类:FileUtil Json工具类:GsonUtils http 工具类:HttpUtil
org.springframework.web.client.AsyncRestTemplate org.springframework.web.client.RestTemplate HTTP请求工具类,基于以上两个Rest请求工具模板完成封装HTTP请求,包括同步和异步请求的实现。
日期与时间的相互转换,日期与时间戳的相互转换的一个工具类。
下面小编就为大家带来一篇HttpUtils 发送http请求工具类(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
android 网络请求get和post方式的请求方式实例