NutzCN Logo
问答 nutzwk 给模板传参数出现乱码 在tomcat环境下
发布于 2365天前 作者 悍匪 1705 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

只要是给模板穿参数就肯定是乱码 下面是我的post方法 用nutz的Http的post方法不能发送模板消息。求解

	public String sendPost(String param) {
		Wx_config wx = dao.fetch(Wx_config.class, Cnd.where("id", "=", 1));
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
				+ wx.getAccess_token();
		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)");
			conn.setRequestProperty("Content-Type",
					"application/json;charset=UTF-8");
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			out = new PrintWriter(conn.getOutputStream());
			// 发送请求参数
			// log.debug(param);
			out.print(param);
			// flush输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(
					new InputStreamReader(conn.getInputStream()));
			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;

	}
17 回复

tomcat没配好编码?

配置了


<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>

这是URI编码,不是tomcat的编码, 说是的JAVA_OPTS里面的-Dfile.encoding=UTF8

还有 tomcat下启动了两次项目怎么回事?上次让您看了说没什么问题 ,可能是eclipse的问题 我现在放在服务器上还是启动了两次

你要改server.xml?那war包就不要也不能放在webapps目录下

为什么用http.post不能发送模板信息?

能发的啊, 报什么错误呢? PS: 有nutzwx

发送 一切正常 http。post 穿进去的是map 然后 我把map打印出来 数据结构是没问题的 但是就是发送不了模板,但是用我自己写的post就可以发送

代码贴来看看?

public String send(String toUser, String id) throws JSONException {
		Wx_user user = dao.fetch(Wx_user.class,Cnd.where("openid","=",toUser));
		Users users = userService.selectUserById(user.getShopid());
		String uid = selectUid(id);
		Wx_config wx = dao.fetch(Wx_config.class, Cnd.where("id", "=", 1));
		Wx_sendModule module = new Wx_sendModule();
		NutMap maps = new NutMap();

		maps.put("first",new Mould("您有新的订单!","#173177}"));
		//商家名称
		maps.put("keyword1",new Mould(work.getNowDate(),"#173177"));
		//商家电话
		maps.put("keyword2",new Mould("销售订单","#173177"));
		//订单号
		maps.put("keyword3",new Mould("已经成功绑定!","#173177"));
		//状态
		maps.put("keyword4",new Mould(users.getCompany(),"#173177"));
		//总价
		maps.put("keyword5",new Mould(uid,"#173177"));
		maps.put("remark",new Mould("请猛击下方的进入查看!","#173177"));
		module.setTouser(toUser);
		module.setTemplate_id(BaseModule.band);
		module.setData(maps);
		Object obj = Json.fromJson(Json.toJson(module));
		Map<String, Object> map = (Map) obj;
		map.remove("errcode");
		map.remove("msgid");
		String jspn = Json.toJson(map);
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
				+ wx.getAccess_token();
		//String jsons=Http.post(url,map, 2500);
		String jsons = work.sendPost(jspn);
		log.debug("发送绑定订单post:"
				+ "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
				+ wx.getAccess_token() + Json.toJson(map));
		return jsons;
	}

String jsons=Http.post(url,map, 2500);//不注释

Content-Type的原因吧?

因为用nutz的http请求是没问题的,其他的接口都不乱码 给微信的也不乱码 只有这个我写的post不行 应该是我自己的写的post的问题,大佬有啥解决的办法吗,求解啊

额, 用 wxapi.template_send 不行吗??

String jspn = Json.toJson(map); // 顺便打印一下内容看看是否乱码?

不是乱码 发出去之后乱的

试试这样

Json.toJson(obj, JsonFormat.full().setAutoUnicode(true));

6666可以了大佬,感谢感谢

添加回复
请先登陆
回到顶部