代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
Java HTTP工具类
作者:
/ 发布于
2019/7/30
/
984
Java HTTP工具类
package com.redis.sign; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; 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.DefaultHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.*; /** * */ public class HttpClient { public static JSONObject postJson(String reqUrl, JSONObject jsonParam) throws Exception { DefaultHttpClient htClient = new DefaultHttpClient(); HttpPost method = new HttpPost(reqUrl); StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");// 解决中文乱码问题 entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); method.setEntity(entity); HttpResponse result = htClient.execute(method); String resData = EntityUtils.toString(result.getEntity()); JSONObject resJson = JSONObject.parseObject(resData); return resJson; } public static String post(String reqUrl, Map<String, String> reqParams) throws Exception { if (reqParams == null) { reqParams = new HashMap<>(); } System.out.println("---------请求地址:" + reqUrl); System.out.println("---------请求参数:" + JSON.toJSONString(reqParams)); CloseableHttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(reqUrl); try { List<NameValuePair> params = new ArrayList<NameValuePair>(); for (String key : reqParams.keySet()) { params.add(new BasicNameValuePair(key, reqParams.get(key))); } UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, "UTF-8"); /* 设置参数 */ post.setEntity(urlEncodedFormEntity); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); String returnMsg = EntityUtils.toString(entity, "UTF-8"); return returnMsg; } catch (Exception e) { e.printStackTrace(); return null; } finally { /* 释放链接 */ post.releaseConnection(); } } public static String get(String reqUrl, Map<String, String> reqParams) throws Exception { System.out.println("---------请求地址:" + reqUrl); System.out.println("---------请求参数:" + JSON.toJSONString(reqParams)); return execute(reqUrl, reqParams, "GET"); } public static String execute(String reqUrl, Map<String, String> reqParams, String method) throws Exception { return execute(reqUrl, reqParams, method, "application/x-www-form-urlencoded"); } public static String execute(String reqUrl, Map<String, String> reqParams, String method, String contentType) throws Exception { String response = ""; String invokeUrl = reqUrl; URL serverUrl = new URL(invokeUrl); HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection(); conn.setReadTimeout(10000); conn.setRequestMethod(method); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); if (reqParams != null && reqParams.size() > 0) { conn.setRequestProperty("Cookie", reqParams.get("Cookie") + ""); reqParams.remove("Cookie"); } conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); conn.setRequestProperty("Content-Type", contentType); // 传递参数 String content = getStringData(reqParams); byte[] bypes = content.toString().getBytes(); conn.getOutputStream().write(bypes); conn.connect(); InputStream is = conn.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is, "utf-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } response = buffer.toString(); conn.disconnect(); return response; } public static String getStringData(Map<String, String> params) { StringBuffer content = new StringBuffer(); List<String> keys = new ArrayList<String>(params.keySet()); Collections.sort(keys); for (int i = 0; i < keys.size(); i++) { String key = (String) keys.get(i); String value = params.get(key); if (value != null) { content.append((i == 0 ? "" : "&") + key + "=" + value); } else { content.append((i == 0 ? "" : "&") + key + "="); } } return content.toString(); } }
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3