URLConnection发送post请求乱码

news/2024/7/3 12:29:54 标签: java, 乱码, post, 网络, http
http://www.w3.org/2000/svg" style="display: none;">

在用UrlConnection进行网络编程,往往出现请求参数出现中文时,中文乱码,需要在请求中指定请求编码格式。

一、检查

1、检查一下有没有指定编码格式
2、检查使用输出流的方式(是否已转码)

二、代码

java">public static String sendPost(String url, String paramsStr, String encode) {
	OutputStreamWriter 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("charset", encode);
		conn.setRequestProperty("Authorization", "Basic xxxxxx");
		conn.setRequestProperty("Accept-Charset", encode);
		conn.setRequestProperty("contentType", encode);
		// 发送POST请求必须设置如下两行
		conn.setDoOutput(true);
		conn.setDoInput(true);

		out = new OutputStreamWriter(conn.getOutputStream(), encode);
		out.write(paramsStr);
		out.flush();
		// 获取响应
		in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encode));
		String lines;
		StringBuffer sb = new StringBuffer();
		while ((lines = in.readLine()) != null) {
			lines = new String(lines.getBytes(encode), encode);
			lines = URLDecoder.decode(URLDecoder.decode(lines, encode), encode);
			sb.append(lines);
		}
		result = sb.toString();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if (out != null) {
				out.close();
			}
			if (in != null) {
				in.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	return result;
}

三、总结:

  • 设置请求编码格式

    connection.setRequestProperty("Accept-Charset", "GBK");
    connection.setRequestProperty("contentType", "GBK");
    
  • 使用可转码的输出流

    PrintWriter out = new PrintWriter(connection.getOutputStream());

    OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), encode); 
    out.write(paramsStr);
    

    or

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    out.wait(paramsStr.getBytes(encode));
    

一般这种请求,要么就是没有给请求声明编码(客户端:服务器,我支持或我要求返回xxx编码!),要么就是没有在请求前给参数转码(服务器:你个都比,我要UTF的,你客户端是GBK的,你不转码我获取的是乱码阿~)。大多情况都是后者。

四、更多

https://blog.csdn.net/u010001043/article/details/53203576


http://www.niftyadmin.cn/n/796723.html

相关文章

搭建企业git代码版本管理所需工具

此片文章纯属记录一下使用gitlab搭建私有git版本管理的一些工具及概念。 先记录一下概念 git 是一种版本控制系统,是一个命令,是一种工具 github 是一个基于git实现的在线代码托管仓库,包含一个网站界面,向互联网开放 g…

js常用正则

1、中文和英文: ^[a-zA-Z\u4E00-\u9FA5]$ 2、中文、英文、数字: ^[a-zA-Z0-9\u4e00-\u9fa5]$ 3、纯数字: ^[0-9]*$ 4、电话(座机和手机号): (^([0-9]{3,4}-)?[0-9]{7,8}$)|(^(0|86|17951)?(13[0-9]|15[01…

Mysql启动错误:“InnoDB: Error: unable to create temporary file”的解决方法

文章目录Mysql启动错误:“InnoDB: Error: unable to create temporary file”的解决方法1、查看日志方式一,查看windows错误日志方式二,查看mysql错误日志(推荐)2、问度娘3、解决问题Mysql启动错误:“InnoD…

SDOI2011 染色

题目链接:戳我 其实也是一个比较常规的树链剖分的题目,主要不同是多记录一个区间的左端颜色,右端颜色,如果左右区间颜色相同就-1. update:因为还有一个树链剖分,所以还要注意一下,上次划分出来的区域的左端点和当前处理区间的右端…

CodeSmith 一、连接Mysql

下载了codesmith 8,连接Mysql却提示“找不到请求的 .Net Framework Data Provider"。 1,下载MySql.Data.dll:https://dev.mysql.com/downloads/windows/visualstudio/ 下载zip格式的即可,解压后将MySql.Data.dll…

[BZOJ2060/Luogu2996][USACO10NOV]拜访奶牛Visiting Cows

题目链接&#xff1a; BZOJ2060. Luogu2996 简单的树形DP。 设\(f[x][0/1]\)表示在以\(x\)为根的子树内选/不选\(x\)的最大答案。 有转移&#xff1a; \(f[x][0]\sum max(f[y][0],f[y][1])\) \(f[x][1]1\sum f[y][0]\) 时间复杂度 \(O(n)\) 代码&#xff1a; #include <cstd…

技术名词

1、jvm jvm是Java Virtual Machine&#xff08;Java虚拟机&#xff09;的缩写&#xff0c;JVM是一种用于计算设备的规范&#xff0c;它是一个虚构出的计算机&#xff0c;是通过在实际的计算机上仿真模拟各种计算机功能来实现的。 2、rpc 远程服务调用。 3、ZooKeeper ZooKeeper…

货币金融学(3): 衍生品

chapter6 金融衍生工具 (1)高杠杆&#xff1b; (2)虚拟性(买小麦期货不是为了买小麦, 而是为了从小麦价格变动中收益)&#xff1b; (3)高风险&#xff1b; 股票衍生工具, 利率衍生工具, 货币衍生工具&#xff1b; 远期, 期货, 期权, 互换&#xff1b; 场内, 场外衍生工具(分散在…