参考:
https://blog.csdn.net/rl529014/article/details/52878443
https://blog.csdn.net/xuebing1995/article/details/79106316
Qt利用QNetworkAccessManager、QNetworkRequest、QNetworkReply实现网络请求
下面是一个我做的例子,在输入栏里输入要查询的地址,点击确定后下方会显示出请求的返回结果。
头文件:
一个简单的GUI,上面类之外的那一串函数是用来进行汉字到UTF-8的转换(请求的URL中汉字要转为UTF-8)
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication4.h"
#include <QtWidgets/QMainWindow>
#include <QTreeWidget>
#include <QTableWidget>
#include <QtSQL/QSqlQuery>
#include<QtNetwork\qnetworkreply.h>
#include<QtNetwork\qnetworkrequest.h>
#include<QtNetwork\qnetworkaccessmanager.h>
#include <QUrl>
#include <QByteArray>
#include <QTextCodec>
#include<qmessagebox.h>
#include<qdebug.h>
#include <qinputdialog.h>
#include<string>
#include<vector>
#include<iostream>
#include<stdio.h>
#include<windows.h>
using namespace std;
string UrlUTF8(char * str);
void GB2312ToUTF_8(string& pOut, char *pText, int pLen);
void Gb2312ToUnicode(WCHAR* pOut, char *gbBuffer);
void UnicodeToUTF_8(char* pOut, WCHAR* pText);
QString getUTF8(QString a);
class QtGuiApplication4 : public QMainWindow
{
Q_OBJECT
public slots:
void finishedSlot(QNetworkReply*);
void button_ok();
public:
QtGuiApplication4(QWidget *parent = Q_NULLPTR);
private:
Ui::QtGuiApplication4Class ui;
QNetworkAccessManager *m_NetManger;
QNetworkReply* m_Reply;
};
cpp:
看起来挺多的,但大部分都是那几个转换函数
#include "QtGuiApplication4.h"
QtGuiApplication4::QtGuiApplication4(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(button_ok()));
}
void QtGuiApplication4::finishedSlot(QNetworkReply *){
m_Reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
m_Reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (m_Reply->error() == QNetworkReply::NoError)
{
QByteArray bytes = m_Reply->readAll();
QString string = QString::fromUtf8(bytes);
qDebug() << string;
ui.textBrowser->setText(string);
}
else
{
qDebug() << m_Reply->errorString();
}
m_Reply->deleteLater();
}
void QtGuiApplication4::button_ok(){
/*网络连接*/
QString s = getUTF8(ui.lineEdit->text());
//QMessageBox::information(0, QString::fromLocal8Bit("提示"), s);
m_NetManger = new QNetworkAccessManager;
//http://api.map.baidu.com/geocoder/v2/?address=%E6%AD%A6%E6%B1%89%E5%B8%82%E9%B2%81%E7%A3%A8%E8%B7%AF%E4%B8%AD%E5%9B%BD%E5%9C%B0%E8%B4%A8%E5%A4%A7%E5%AD%A6&output=json&ak=bGNSHttgc3VWi8E7tsymT6qjEwVCM5rN&callback=showLocation
QUrl url("http://api.map.baidu.com/geocoder/v2/?address="+s+"&output=json&ak=bGNSHttgc3VWi8E7tsymT6qjEwVCM5rN&callback=showLocation");http://api.map.baidu.com/geocoder/v2/?address=%E6%AD%A6%E6%B1%89%E5%B8%82%E9%B2%81%E7%A3%A8%E8%B7%AF388%E5%8F%B7%E4%B8%AD%E5%9B%BD%E5%9C%B0%E8%B4%A8%E5%A4%A7%E5%AD%A6&output=json&ak=bGNSHttgc3VWi8E7tsymT6qjEwVCM5rN&callback=showLocation
m_Reply = m_NetManger->get(QNetworkRequest(url));
QObject::connect(m_NetManger, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedSlot(QNetworkReply*)));
}
string UrlUTF8(char * str)
{
string tt;
string dd;
GB2312ToUTF_8(tt, str, strlen(str));
int len = tt.length();
for (int i = 0; i<len; i++)
{
if (isalnum((BYTE)tt.at(i))) //判断字符中是否有数组或者英文
{
char tempbuff[2] = { 0 };
sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
dd.append(tempbuff);
}
else if (isspace((BYTE)tt.at(i)))
{
dd.append("+");
}
else
{
char tempbuff[4];
sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
dd.append(tempbuff);
}
}
return dd;
}
void GB2312ToUTF_8(string& pOut, char *pText, int pLen)
{
char buf[4];
memset(buf, 0, 4);
pOut.clear();
int i = 0;
while (i < pLen)
{
//如果是英文直接复制就可以
if (pText[i] >= 0)
{
char asciistr[2] = { 0 };
asciistr[0] = (pText[i++]);
pOut.append(asciistr);
}
else
{
WCHAR pbuffer;
Gb2312ToUnicode(&pbuffer, pText + i);
UnicodeToUTF_8(buf, &pbuffer);
pOut.append(buf);
i += 2;
}
}
return;
}
void Gb2312ToUnicode(WCHAR* pOut, char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, gbBuffer, 2, pOut, 1);
return;
}
void UTF_8ToUnicode(WCHAR* pOut, char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return;
}
void UnicodeToUTF_8(char* pOut, WCHAR* pText)
{
// 注意 WCHAR高低字的顺序,低字节在前,高字节在后
char* pchar = (char *)pText;
pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}
QString getUTF8(QString a){
/*将QString转为string再转char*/
char *str;
string s1 = string((const char *)a.toLocal8Bit());
int len = s1.length();
str = (char *)malloc((len + 1)*sizeof(char));
s1.copy(str, len, 0);
//char*转UTF-8下的QString
string s = "";
s = UrlUTF8(str);
return QString::fromStdString(s);
}
main:
#include "QtGuiApplication4.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtGuiApplication4 w;
w.show();
return a.exec();
}