Postman|Qt笔记-解决Could not get any response Myth丶恋晨 2022-08-28 08:45 1阅读 0赞 这里Fiddler抓包是有显示的: ![watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBASVQxOTk1_size_20_color_FFFFFF_t_70_g_se_x_16][] 但Postman却提示Could not get any response ![watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBASVQxOTk1_size_20_color_FFFFFF_t_70_g_se_x_16 1][] 经过各种分析,发现问题出现在服务端 ![watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBASVQxOTk1_size_18_color_FFFFFF_t_70_g_se_x_16][] 主要的原因就是Content-Length这个值,这里的已经告诉客户端是utf-8编码的,但里面含有中文,对应的数据Content-Length与body里面的长度不一致。 服务端是这样计算长度的: class HttpResponse { public: static QString success(const QString &body){ QString ret = "HTTP/1.1 200 OK\r\n"\ "Date: %1\r\n"\ "Pragma: no-cache\r\n"\ "Content-Type: application/json; charset=utf-8\r\n"\ "Cache-Control: no-cache\r\n"\ "Server: It1995HttpsWebServer\r\n"\ "Content-Length: %2\r\n"\ "\r\n"\ "%3"; QLocale locale = QLocale::English; QString format = "ddd,dd MM yyyy hh:mm:ss"; QString currentTime =locale.toString(QDateTime::currentDateTime().toUTC(), format) + " GMT"; int length = body.size(); ret = ret.arg(currentTime).arg(length).arg(body); return ret; } }; 这种方式不正确,没有转换成utf-8计算长度,正确的代码: class HttpResponse { public: static QString success(const QString &body){ QString ret = "HTTP/1.1 200 OK\r\n"\ "Date: %1\r\n"\ "Pragma: no-cache\r\n"\ "Content-Type: application/json; charset=utf-8\r\n"\ "Cache-Control: no-cache\r\n"\ "Server: It1995HttpsWebServer\r\n"\ "Content-Length: %2\r\n"\ "\r\n"\ "%3"; QLocale locale = QLocale::English; QString format = "ddd,dd MM yyyy hh:mm:ss"; QString currentTime =locale.toString(QDateTime::currentDateTime().toUTC(), format) + " GMT"; int length = body.toUtf8().size(); ret = ret.arg(currentTime).arg(length).arg(body); return ret; } }; 关键: ![watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBASVQxOTk1_size_20_color_FFFFFF_t_70_g_se_x_16 2][] [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBASVQxOTk1_size_20_color_FFFFFF_t_70_g_se_x_16]: /images/20220828/ed526090ecfa46849d5b5ab6a7072989.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBASVQxOTk1_size_20_color_FFFFFF_t_70_g_se_x_16 1]: /images/20220828/d9055e26dcb64cabac8cd35cfee492fe.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBASVQxOTk1_size_18_color_FFFFFF_t_70_g_se_x_16]: /images/20220828/7e31005a8692414c80b033bfedd9abeb.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBASVQxOTk1_size_20_color_FFFFFF_t_70_g_se_x_16 2]: /images/20220828/799c05dd564646d0a563b86db394b111.png
还没有评论,来说两句吧...