java 中http请求为了防止乱码解决方案
作者:lqh 时间:2023-08-09 07:59:33
java 中http请求为了防止乱码解决方案
今天做一个与地图有关的项目,需要发起http请求地图数据
写了一个工具类,希望大家都能用上吧
package com.lrdd.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class HttpRequest {
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"utf-8"));
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(),"utf-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:java,http请求,乱码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring scheduled单线程和多线程使用过程中的大坑
2022-09-24 05:51:10
JDK13的新特性之AppCDS详解
2023-03-19 10:32:38
Java基础之finally语句与return语句详解
2021-11-27 19:21:22
Java过滤器Filter详解
2022-06-27 04:08:44
![](https://img.aspxhome.com/file/2023/5/100065_0s.png)
WPF自定义控件的实现
2021-07-06 02:34:11
![](https://img.aspxhome.com/file/2023/5/74825_0s.jpg)
Android去除AlertDialog的按钮栏的分隔线
2021-06-06 18:53:54
![](https://img.aspxhome.com/file/2023/9/94289_0s.png)
Android实战教程第九篇之短信高效备份
2022-02-14 12:59:20
Java并发编程之同步容器
2023-03-10 16:34:29
![](https://img.aspxhome.com/file/2023/8/70308_0s.png)
java简易小游戏制作代码
2023-11-23 19:49:38
![](https://img.aspxhome.com/file/2023/8/59048_0s.jpg)
Android中使用CircleImageView和Cardview制作圆形头像的方法
2022-04-19 05:41:35
![](https://img.aspxhome.com/file/2023/2/92582_0s.gif)
java获取网络图片上传到OSS的方法
2023-10-14 23:01:07
Java实现去除文档阴影的示例代码
2023-08-31 11:45:48
![](https://img.aspxhome.com/file/2023/5/58415_0s.png)
浅谈java面向对象中四种权限
2023-03-09 18:43:46
详解Winform里面的缓存使用
2022-07-22 21:16:32
![](https://img.aspxhome.com/file/2023/4/104344_0s.png)
Java如何实现http接口参数和返回值加密
2023-08-23 12:28:37
Android Handler消息机制分析
2023-10-01 13:22:26
![](https://img.aspxhome.com/file/2023/8/87268_0s.jpg)
Android中使用Matrix控制图形变换和制作倒影效果的方法
2022-11-23 07:35:23
![](https://img.aspxhome.com/file/2023/8/137558_0s.png)
Java中String的JdbcTemplate连接SQLServer数据库的方法
2022-09-05 00:34:12
Android 4.4.2 横屏应用隐藏状态栏和底部虚拟键的方法
2022-07-14 13:19:41
springboot实现注册加密与登录解密功能(demo)
2021-12-15 15:10:08
![](https://img.aspxhome.com/file/2023/4/77484_0s.png)