Qt HTTP服务器会自动把BASE64编码中的+号删除掉,捣鼓了几个钟头,只需要加一个参数即可了。加密参数设置:Base64UrlEncoding标志
QString TQmlHelper::add64(QVariant data)
{
QString a = data.toByteArray().toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals).toStdString().c_str();
qDebug()<<"加密:"<<a;
return a;
}
QString TQmlHelper::sub64(QVariant data)//传进来的是已近base64加密的数据
{
QByteArray a = QByteArray::fromBase64(data.toByteArray(),QByteArray::Base64UrlEncoding QByteArray::OmitTrailingEquals);
QString xx = QString::fromUtf8(a);
return xx;
}
Qt的富文本编辑器和文本当设置Text.RichText属性,text值是纯粹的HTML编码,此时发送给服务器需要BASE64转换。为何我需要发送完整数据,做一个聊天控件,对方的label也是富文本HTTP代码格式,这样会很方便和强大.
**发送BASE64
***此时用QML的富文本作为测试,此时获取QTextArea的text值为:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">001</p></body></html>
qml: A:PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMC8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9UUi9SRUMtaHRtbDQwL3N0cmljdC5kdGQiPgo8aHRtbD48aGVhZD48bWV0YSBuYW1lPSJxcmljaHRleHQiIGNvbnRlbnQ9IjEiIC8+PHN0eWxlIHR5cGU9InRleHQvY3NzIj4KcCwgbGkgeyB3aGl0ZS1zcGFjZTogcHJlLXdyYXA7IH0KPC9zdHlsZT48L2hlYWQ+PGJvZHkgc3R5bGU9IiBmb250LWZhbWlseTonU2ltU3VuJzsgZm9udC1zaXplOjlwdDsgZm9udC13ZWlnaHQ6NDAwOyBmb250LXN0eWxlOm5vcm1hbDsiPgo8cCBzdHlsZT0iIG1hcmdpbi10b3A6MHB4OyBtYXJnaW4tYm90dG9tOjBweDsgbWFyZ2luLWxlZnQ6MHB4OyBtYXJnaW4tcmlnaHQ6MHB4OyAtcXQtYmxvY2staW5kZW50OjA7IHRleHQtaW5kZW50OjBweDsiPjAwMTwvcD48L2JvZHk+PC9odG1sPg==,B:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">001</p></body></html>
Flickable {
id: flickable
x:offsetBeginX
clip: true
anchors.bottom: parent.bottom
width: parent.width - btn.width -offsetBeginX-offsetendx
height: btn.height
TextArea.flickable: TextArea {
id:edit
text: ""
placeholderText: "输入讯息"
wrapMode: TextArea.Wrap
textFormat: TextEdit.RichText
onCursorPositionChanged: {
scrol.decrease()
}
onTextChanged: {
setrect();
}
onFocusChanged: {
if(focus == true)
{
setrect()
cursorPosition = length
}
}
function setrect()
{
if(contentHeight>btn.height)
{
if(contentHeight<180)
{
flickable.height = contentHeight
}else
{
flickable.height = 180
}
}
}
}
ScrollBar.vertical: ScrollBar {
id:scrol
active: true
pressed: true
size: 1.0
}
}
若是想发送完整的数据,则就不得不提base64编码
Button{
x:flickable.x+flickable.width + offsetendx
anchors.bottom: parent.bottom
id:btn
text:"发送"
font.bold: true
font.pointSize: 14
height: width*0.75
width: 80
onClicked: {
All.webView.runJavaScript("
SendmessageToOne("+ "'"+ All.当前聊天对象.zh+ "'"+ "," +"'"+ myapp.add64(edit.text)+"'"+")")
msgmodel.append({txt:edit.text,org:1})
msgroot.adjustIndex();
reset();
//发送他后把这条内容加到聊天中
}
}
//解码BASE64
function 获取新消息(msg)
{
console.log("获取:"+msg)
console.log("获取转换:"+myapp.sub64(msg))
rootstatcktongxinj.currentItem.msgmodel.append({txt:myapp.sub64(msg),org:0})
rootstatcktongxinj.currentItem.adjustIndex();
}