NutzCN Logo
问答 request用post提交乱码的问题
发布于 80天前 作者 书生 145 次浏览 复制 上一个帖子 下一个帖子
标签:

网上的解决方案

String name = request.getParameter("name”);//得到乱码    
                          name = new String(name.getBytes("iso-8859-1"),"utf-8”);//得到正常的name值     

iOS提交的数据,用这方法确实可以解决乱码,但是Android提交上来的数据就乱码了,能不能统一解决一下呢?

19 回复

ios提交的时候没设置编码?

那就是没设置好了咯

有一种可能性是ios的代码把参数放在url了

那这个问题怎么解决?

感觉网上所说也都是这样解释的,放在url后面会导致这个问题

在tomcat中设置了这个也没效果

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

打印一下req的headers,看看有啥区别

16:39:45.155 DEBUG (ForumModule.java:290) addTopic1 - -----------Api-Key: a01e52f3275a646546d17e07fc12ca47196e08a9:UTF-8--a01e52f3275a646546d17e07fc12ca47196e08a9
16:39:45.156 DEBUG (ForumModule.java:290) addTopic1 - -----------Api-Nonce: F7CA832D-AB15-46FE-A7F6-E2C32285825C:UTF-8--F7CA832D-AB15-46FE-A7F6-E2C32285825C
16:39:45.156 DEBUG (ForumModule.java:290) addTopic1 - -----------Api-Time: 1499330387396:UTF-8--1499330387396
16:39:45.156 DEBUG (ForumModule.java:290) addTopic1 - -----------Api-Loginname: admin:UTF-8--admin
16:39:45.156 DEBUG (ForumModule.java:290) addTopic1 - -----------title: ?????????????????????:UTF-8--你也想不想看更
16:39:45.157 DEBUG (ForumModule.java:290) addTopic1 - -----------content: ?????????è??è??è???????????:UTF-8--你也来试试这个世界
16:39:45.157 DEBUG (ForumModule.java:290) addTopic1 - -----------forumId: dd2e927663294872bae0801d6eb081c0:UTF-8--dd2e927663294872bae0801d6eb081c0
16:39:45.156 DEBUG (CachePubSub.java:15) onPMessage - channel=LCache:shiro-activeSessionCache, msg=tak68gr9h4ihppv47d9156stb0:snicsqamasjqhrk2hr74a14kkb pattern=LCache:*
16:46:15.068 DEBUG (ForumModule.java:295) addTopic1 - key=host value=www.ssu8.cn:8080
16:46:15.068 DEBUG (ForumModule.java:295) addTopic1 - key=accept value=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
16:46:15.068 DEBUG (ForumModule.java:295) addTopic1 - key=connection value=keep-alive
16:46:15.069 DEBUG (ForumModule.java:295) addTopic1 - key=accept-language value=zh-cn
16:46:15.069 DEBUG (ForumModule.java:295) addTopic1 - key=accept-encoding value=gzip, deflate
16:46:15.069 DEBUG (ForumModule.java:295) addTopic1 - key=content-type value=application/x-www-form-urlencoded
16:46:15.069 DEBUG (ForumModule.java:295) addTopic1 - key=origin value=http://192.168.1.108:8081
16:46:15.070 DEBUG (ForumModule.java:295) addTopic1 - key=content-length value=446
16:46:15.070 DEBUG (ForumModule.java:295) addTopic1 - key=upgrade-insecure-requests value=1
16:46:15.070 DEBUG (ForumModule.java:295) addTopic1 - key=user-agent value=Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Mobile/14E304
16:46:15.070 DEBUG (ForumModule.java:295) addTopic1 - key=referer value=http://192.168.1.108:8081/

解决方案,

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

在tomcat的server.xml中设置URIEncoding编码

猜测原因:
iOS内核对post请求做了优化,当发送的内容比较少时,发送方式类似get请求,当内容多到一定程度时,就用post请求

不会的,肯定是ios端写错

ios端和Android端都是用HTML5实现的,使用的方法都是一样的,Android就不会有乱码,iOS的就会出现乱码,我做实验时,内容比较短的时候会出现乱码,内容长的时候就不会,Android不论长还是短都正常
@wendal

具体的代码发一下看看,页面端

前端页面用vue.js

 release: function () {

        if (!this.$route.query.forumId) {
          this.dialog.type = 'error'
          this.dialog.text = '请勿直接访问,将无法发帖!!!'
          this.dialog.show = true
          return
        }

        if (trim(this.title) === '') {
          this.dialog.type = 'error'
          this.dialog.text = '标题不能为空呢 /(ㄒoㄒ)/~~'
          this.dialog.show = true
          return
        }

        if (trim(this.content.innerHTML) === '') {
          this.dialog.type = 'error'
          this.dialog.text = '内容不能为空呢 /(ㄒoㄒ)/~~'
          this.dialog.show = true
          return
        }
        getAppSignInfo()
        window.callbackFromApp = (methodName, result, extInfo) => {
        	if (methodName == 'getAppSignInfo') {
        		let data = JSON.parse(result)

              postJsonp(config.ajaxUrl + 'forum/app/addTopic', {
                'Api-Key': data['Api-Key'],
                'Api-Nonce': data['Api-Nonce'],
                'Api-Time': data['Api-Time'],
                'Api-Loginname': data['Api-Loginname'],
                title: this.title,
                content: this.content.innerHTML,
                forumId: this.$route.query.forumId,
                iType: this.tags
              }, (responese) => {
                if (responese.ok === 1) {
                  this.dialog.type = 'success'
                  this.dialog.text = '哇哈哈,发帖成功了~~~ '
                  this.dialog.show = true
                  this.content.innerHTML = ''
                  this.title = ''
                  this.tags = ''
                  this.$router.push({ name: 'forum', params: { forumId: this.$route.query.forumId } })
                } else {
                  this.dialog.type = 'error'
                  this.dialog.text = responese.msg
                  this.dialog.show = true
                }
              }, (error) => {
                this.dialog.type = 'error'
                this.dialog.text = '发帖失败!!!网络不给力呢~~~ /(ㄒoㄒ)/~~'
                this.dialog.show = true
              })

    

UI-黄海宝 2017/7/7 上午 9:16:53 export function postJsonp (url, data, fn) { let form = document.createElement('form'); form.id = form.name = 'postForm'; if (data) { for (let key in data) { let input = document.createElement('input'); input.type = 'hidden'; input.name = key; input.value = data[key]; form.appendChild(input); } } let iframe = null; try { iframe = document.createElement('<iframe name="postIframe">'); } catch (e) { iframe = document.createElement('iframe'); } iframe.id = iframe.name = 'postIframe'; iframe.style.display = 'none'; document.body.appendChild(iframe); document.body.appendChild(form); form.action = url; form.target = iframe.name; form.method = 'post'; form.charset = 'UTF-8' // form.acceptCharset = 'iso8859-1' form.submit(); if (iframe.attachEvent) { iframe.attachEvent('onload', _loadFn); } else { iframe.onload = _loadFn; } iframe.state = 0; function _loadFn () { if (iframe.state === 1) { let data = ''; try { data = iframe.contentWindow.name; } catch (e) { console.log(e); } let json = '' try { json = JSON.parse(data); } catch (e) { console.log(e); } _callback(json) iframe.onload = null; document.body.removeChild(iframe); document.body.removeChild(form); } else if (iframe.state === 0) { iframe.state = 1; iframe.src = ''; } } function _callback (json) { if (fn && typeof fn === 'function') { fn(json); } else { let svalue = url.match(new RegExp('[?|&]callback=([^&]*)(&?)')); fn = window[svalue ? svalue[1] : svalue]; if (fn && typeof fn === 'function') { fn(json); } } } return false; }

@wendal

好麻烦,jquery不能用?

sorry,我是写后端的,前端有专人写的,前后端分离的

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