找回密码
 立即注册
收起左侧

http上传文件问题

5
回复
114
查看
[复制链接]
累计签到:456 天
连续签到:2 天
来源: 2019-10-30 10:13:57 显示全部楼层 |阅读模式
100Qter豆
本帖最后由 红尘氵梦 于 2019-10-30 11:13 编辑

在用以下方式进行请求的时候,第一个post没问题,第二个post就不能得到正确的结果,一直返回{"code":-5990,"message":"ERROR_CGI_MISSING_FILECONTENT"}提示,请问第二次post代码有什么问题?

POST /album/getSign.json HTTP/1.1
Host: www.dianxiaomi.com
Connection: keep-alive
Content-Length: 31
Accept: application/json, text/javascript, */*; q=0.01
Origin: https://www.dianxiaomi.com
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
Sec-Fetch-Mode: cors
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Sec-Fetch-Site: same-origin
Referer: https://www.dianxiaomi.com/shopeeProduct/add.htm
Accept-Encoding: gzip, deflate, br
Accept-Language: zh,en;q=0.9,zh-CN;q=0.8
Cookie: 

bucket=wxalbum&fileName=2-0.jpg



HTTP/1.1 200 
Server: nginx
Date: Wed, 30 Oct 2019 01:58:05 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Vary: Accept-Encoding
Set-Cookie: JSESSIONID=942623D483D6EF12EDD63F3A0E45BA13; Path=/; Secure; HttpOnly
Set-Cookie: rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Tue, 29-Oct-2019 01:58:05 GMT
Set-Cookie: 
Content-Encoding: gzip
Connection: keep-alive

{"code":0,"sign":"3hQfpHVtD2QHNN0AyLhw6c1L4oFhPTEwMDAxNjU4JmI9d3hhbGJ1bSZrPUFLSUREamI3eVNZUjYyTXNyVFBKVlVkRlZoc2dOUU1RODZ3TCZlPTE1NzI0MDEyODUmdD0xNTcyNDAwNjg1JnI9MTE5OTk0NTA4NCZ1PTAmZj0=","url":"//web.image.myqcloud.com/photos/v2/10001658/wxalbum/0/wxalbum%2F301077%2F20191030095805%2F1124f305d4b0a825c37e18e2feb29113.jpg","fileId":"wxalbum/301077/20191030095805/1124f305d4b0a825c37e18e2feb29113.jpg"}



POST /photos/v2/10001658/wxalbum/0/wxalbum%2F301077%2F20191030095805%2F1124f305d4b0a825c37e18e2feb29113.jpg?sign=3hQfpHVtD2QHNN0AyLhw6c1L4oFhPTEwMDAxNjU4JmI9d3hhbGJ1bSZrPUFLSUREamI3eVNZUjYyTXNyVFBKVlVkRlZoc2dOUU1RODZ3TCZlPTE1NzI0MDEyODUmdD0xNTcyNDAwNjg1JnI9MTE5OTk0NTA4NCZ1PTAmZj0%3D HTTP/1.1
Host: web.image.myqcloud.com
Connection: keep-alive
Content-Length: 124072
Sec-Fetch-Mode: cors
Origin: https://www.dianxiaomi.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarycEC80r1ndG8rgBDB
Accept: */*
Sec-Fetch-Site: cross-site
Referer: https://www.dianxiaomi.com/shopeeProduct/add.htm
Accept-Encoding: gzip, deflate, br
Accept-Language: zh,en;q=0.9,zh-CN;q=0.8

------WebKitFormBoundarycEC80r1ndG8rgBDB
Content-Disposition: form-data; name="FileContent"; filename="2-0.jpg"
Content-Type: image/jpeg

图片文件.jpg

------WebKitFormBoundarycEC80r1ndG8rgBDB
Content-Disposition: form-data; name="MagicContext"

{"fullcid":"","isNeedTree":0,"fileName":"2-0.jpg"}
------WebKitFormBoundarycEC80r1ndG8rgBDB--

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 30 Oct 2019 01:58:12 GMT
Content-Type: */*
Content-Length: 444
Access-Control-Allow-Origin: *
Connection: keep-alive

{"code":0,"message":"SUCCESS","data":{"download_url":"http://wxalbum-10001658.image.myqcloud.com/wxalbum/301077/20191030095805/1124f305d4b0a825c37e18e2feb29113.jpg","fileid":"wxalbum/301077/20191030095805/1124f305d4b0a825c37e18e2feb29113.jpg","info":[{"0":{"height":800,"width":800}}],"photo_rgb":"0x6a1819","url":"http://web.image.myqcloud.com/photos/v2/10001658/wxalbum/0/wxalbum/301077/20191030095805/1124f305d4b0a825c37e18e2feb29113.jpg"}
}

QString TestQt::GetPostUrl(QString strName)
{
        QString strUrl = "https://www.dianxiaomi.com/album/getSign.json";
        QString strPostData = QString("bucket=wxalbum&fileName=%1").arg(strName);
        QSslConfiguration config;
        config.setPeerVerifyMode(QSslSocket::VerifyNone);
        config.setProtocol(QSsl::TlsV1_0);
        QNetworkRequest req(strUrl);
        req.setRawHeader("Cookie", m_strCookie.toUtf8());
        req.setRawHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        //req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
        req.setSslConfiguration(config);
        QNetworkAccessManager *managerNetwork = new QNetworkAccessManager;
        QNetworkReply *reply = managerNetwork->post(req, strPostData.toUtf8());

        //槽函数可以用带QEventLoop去读取数据,也可以用自定义槽函数//这个是异步操作
        QEventLoop eventLoop;
        connect(managerNetwork, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
        eventLoop.exec();

        QString strResponse = reply->readAll();
        delete managerNetwork;
        if (strResponse.isEmpty())
        {
                qDebug() << "创建页面失败!";
                return false;
        }

        QJsonParseError json_error;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(strResponse.toUtf8(), &json_error);
        if (json_error.error != QJsonParseError::NoError)
        {
                return "";
        }

        if (!jsonDoc.isObject())
        {
                return "";
        }

        QJsonObject objRoot = jsonDoc.object();
        if (!objRoot.contains("url"))
        {
                return "";
        }

        QString strPostUrl = objRoot.value("url").toString();

        if (!objRoot.contains("sign"))
        {
                return "";
        }

        QString strSign = objRoot.value("sign").toString();
        strSign.replace("+", "%2B");
        strSign.replace("/", "%2F");
        strSign.replace("=", "%3D");
        QString strTemp = QString("https:%1?sign=%2").arg(strPostUrl, strSign);

        return strTemp;
}

void TestQt::GetPictureUrl(const QString & strName, QString & strPath)
{
        QString strUrl = GetPostUrl(strName);
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

        QHttpPart partPicture;
        QString contentVariant1 = QString(" form-data; name=\"FileContent\"; filename=\"%1\"").arg(strName);
        partPicture.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(contentVariant1));
        partPicture.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
        QFile* file=new QFile(strPath);
        bool bRet = file->open(QIODevice::ReadOnly);
        if (!bRet)
        {
                return;
        }
        partPicture.setBodyDevice(file);
        file->setParent(multiPart);

        QHttpPart partScene;
        partScene.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(" form-data; name=\"MagicContext\""));
        QString contentVariant = QString("{\"fullcid\":\"\",\"isNeedTree\":0,\"fileName\":\"%1\"}").arg(strName);
        partScene.setBody(contentVariant.toUtf8());

        QSslConfiguration config;
        config.setPeerVerifyMode(QSslSocket::VerifyNone);
        config.setProtocol(QSsl::TlsV1_0);
        QNetworkRequest req(strUrl);
        req.setSslConfiguration(config);

        multiPart->append(partPicture);
        multiPart->append(partScene);
        QNetworkAccessManager *managerNetwork = new QNetworkAccessManager;
        QNetworkReply *reply = managerNetwork->post(req, multiPart);
        multiPart->setParent(reply);

        //槽函数可以用带QEventLoop去读取数据,也可以用自定义槽函数//这个是异步操作
        QEventLoop eventLoop;
        connect(managerNetwork, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
        eventLoop.exec();

        QString strResponse = reply->readAll();
        delete managerNetwork;
        if (strResponse.isEmpty() || !strResponse.contains("code\":0"))
        {
                qDebug() << strResponse;
                return;
        }

        QJsonParseError json_error;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(strResponse.toUtf8(), &json_error);
        if (json_error.error != QJsonParseError::NoError)
        {
                return;
        }

        if (!jsonDoc.isObject())
        {
                return;
        }

        QJsonObject objRoot = jsonDoc.object();
        if (!objRoot.contains("data"))
        {
                return;
        }

        QJsonValue valueRoot = objRoot.value("data");
        if (!valueRoot.isObject())
        {
                return;
        }

        QJsonObject obj = valueRoot.toObject();
        if (!obj.contains("download_url"))
        {
                return;
        }

        QString strPathTemp = obj.value("download_url").toString();
}

最佳答案

查看完整内容

不能~ 要有个人回复才行
阿里云
回复

使用道具 举报

累计签到:170 天
连续签到:2 天
2019-10-30 10:13:58 显示全部楼层
红尘氵梦 发表于 2019-10-30 14:45
问:自己回答了不能结帖吗?

不能~  要有个人回复才行

点评

这样的设计是不合理的,哎哎。那就给你了  详情 回复 发表于 2019-10-31 09:46
回复

使用道具 举报

累计签到:456 天
连续签到:2 天
2019-10-30 14:43:36 显示全部楼层
找到原因了,是因为QHttpMultiPart设置请求头中默认的Boundary值会有引号
如:
Content-Type: multipart/form-data; boundary="----WebKitFormBoundary7MA4YWxkTrZu0gW"


而那个网站的请求不能有引号
如:
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW


所以代码可以如下:
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
        multiPart->setBoundary("----WebKitFormBoundary7MA4YWxkTrZu0gW");

QNetworkRequest req(strUrl);
req.setRawHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
回复

使用道具 举报

累计签到:456 天
连续签到:2 天
2019-10-30 14:45:48 显示全部楼层
问:自己回答了不能结帖吗?

点评

不能~ 要有个人回复才行  详情 回复 发表于 2019-10-31 09:17
回复

使用道具 举报

累计签到:456 天
连续签到:2 天
2019-10-31 09:46:04 显示全部楼层
驲屋安阮 发表于 2019-10-31 09:17
不能~  要有个人回复才行

这样的设计是不合理的,哎哎。那就给你了

点评

谢谢大佬!  详情 回复 发表于 2019-10-31 09:49
回复

使用道具 举报

累计签到:170 天
连续签到:2 天
2019-10-31 09:49:33 显示全部楼层
红尘氵梦 发表于 2019-10-31 09:46
这样的设计是不合理的,哎哎。那就给你了

谢谢大佬!                           
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册