概述
简易版Http请求工具
- 简易版Http请求工具
- GET请求
- POST请求
- 完整代码
简易版Http请求工具
这里的联网应用程序使用java.net包下的URL/HttpURLConnection类实现get/post请求
1、创建一个RUL对象
URL url = new URL(reqUrl);
2、获取URLConnection实例,与URL引用的远程对象的连接。
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
3、HttpURLConnection打开指向此引用的资源的通信链接
conn.connect();
4、从此打开的连接读取的输入流。
conn.getInputStream()
5、处理输入流
根据业务处理输入流
GET请求
关键代码
public static String doGet(URL url, String encoding) throws IOException {
BufferedReader reader = null;
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection)url.openConnection();
setConnectionParameter("GET", conn);
setHeader(encoding, conn);
conn.connect();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
return getResult(reader);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (reader != null){
reader.close();
}
if (conn != null){
conn.disconnect();
}
}
}
POST请求
关键代码
public static String doPost(URL url, Map<String, String> params, String encoding) throws IOException {
DataOutputStream out = null;
BufferedReader reader = null;
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection)url.openConnection();
setConnectionParameter("POST", conn);
setHeader(encoding, conn);
conn.connect();
if ((null != params) && (!params.isEmpty())) {
out = new DataOutputStream(conn.getOutputStream());
String stringParams = buildParams(params);
out.write(stringParams.getBytes());
out.flush();
}
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
return getResult(reader);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (reader != null){
reader.close();
}
if (out != null){
out.close();
}
if (conn != null){
conn.disconnect();
}
}
}
完整代码
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class HttpUtils {
private static final String CHARSET = "UTF-8";
public static String doGet(String reqUrl) throws IOException {
return doGet(reqUrl, CHARSET);
}
public static String doGet(String reqUrl, String encoding) throws IOException {
URL url = new URL(reqUrl);
return doGet(url, encoding);
}
public static String doGet(URL url, String encoding) throws IOException {
BufferedReader reader = null;
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection)url.openConnection();
setConnectionParameter("GET", conn);
setHeader(encoding, conn);
conn.connect();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
return getResult(reader);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (reader != null){
reader.close();
}
if (conn != null){
conn.disconnect();
}
}
}
public static String doPost(String reqUrl, Map<String, String> params) throws IOException {
return doPost(reqUrl, params, CHARSET);
}
public static String doPost(String reqUrl, Map<String, String> params, String encoding) throws IOException {
URL url = new URL(reqUrl);
return doPost(url, params, encoding);
}
public static String doPost(URL url, Map<String, String> params, String encoding) throws IOException {
DataOutputStream out = null;
BufferedReader reader = null;
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection)url.openConnection();
setConnectionParameter("POST", conn);
setHeader(encoding, conn);
conn.connect();
if ((null != params) && (!params.isEmpty())) {
out = new DataOutputStream(conn.getOutputStream());
String stringParams = buildParams(params);
out.write(stringParams.getBytes());
out.flush();
}
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
return getResult(reader);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (reader != null){
reader.close();
}
if (out != null){
out.close();
}
if (conn != null){
conn.disconnect();
}
}
}
/**
* 设置连接参数
* @param method
* @param conn
* @throws IOException
*/
private static void setConnectionParameter(String method, HttpURLConnection conn) throws IOException {
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setInstanceFollowRedirects(true);
conn.setConnectTimeout(1000*10);
conn.setReadTimeout(1000*10);
conn.setRequestMethod(method);
}
/**
* 设置请求属性
* @param encoding
* @param conn
*/
private static void setHeader(String encoding, HttpURLConnection conn) {
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("accept-language", "zh_CN");
conn.setRequestProperty("Charset", encoding);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//伪装浏览器
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36");
}
private static String getResult(BufferedReader reader) throws IOException {
StringBuffer buff = new StringBuffer();
String line = reader.readLine();
while (line != null) {
//
buff.append("n"+ line);
buff.append(line);
line = reader.readLine();
}
String result = buff.toString();
return result;
}
private static String buildParams(Map<String, String> params) {
if (null == params || params.isEmpty()){
return "";
}
StringBuilder sb = new StringBuilder();
int i = 0;
for (Map.Entry entry : params.entrySet()) {
if (i > 0){
sb.append("&");
}
sb.append((String)entry.getKey());
sb.append("=");
sb.append((String)entry.getValue());
i++;
}
return sb.toString();
}
}
最后
以上就是爱笑路灯为你收集整理的简易版Http请求工具简易版Http请求工具的全部内容,希望文章能够帮你解决简易版Http请求工具简易版Http请求工具所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复