php http请求返回内容不正确,前面多了字符串
本地服务启动无任何异常,但是c++客户端 请求 pph服务器结果一直格式无法解析,但是使用postMan等测试工具均正常,php返回之前结果,肉眼看确实木有问题。
起先觉得有点怪,但是抓包结果中也是现实结构前面多了一点东西,这特么就尴尬了不是,一看就知道是编码问题,但是,,,
我用Python实现了下,
"""验证登录""" def on_req_login(self, uname, upwd, uhost, uport): print("on_req_login") # json串数据使用 # textmod = {"username": "ca1", "password": "000000", "params": {"user": uname, "password": upwd}, "id": 1} textmod = {"username": uname, "password": upwd, "opcode": "0"} textmod = json.dumps(textmod).encode(encoding='utf8') # # 普通数据使用 # textmod = parse.urlencode(textmod).encode(encoding='utf-8') print(textmod) header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',"Content-Type": "application/json"} url = 'http://'+uhost+'/vesystem/index.php/VDI/Index/voi_client_OP' req = request.Request(url=url, data=textmod, headers=header_dict) res = request.urlopen(req) res = res.read() print("请求》》》") print(res) print("unicode_escape》》》") resJSON = res.decode('unicode_escape') print(resJSON) # print("utf-8》》》") # retdata = res.decode(encoding='utf-8') # print(retdata) return resJSON
结构返回:大写的MMP
程序崩溃,就是个数不正确,当然这个可以try一下,这种方式先不考虑,,,
可以清楚的看到返回的结果前面是有一点东西的,还以为是php配置什么造成的,再次检查了一下代码和php配置问题,尴尬尴尬尴尬,忽然记起来之前java和安卓交互出现过类似现象
最终修改文件编码修复问题,修改为UTF-8无BOM格式,建议服务器环境下不要用记事本打开文件,这个是由于测试同学用记事本打开过文件造成的影响
返回结果正常
修改文件编码是解决问题的一种方式
第二种方式就是接收端处理,在格式化json之前替换掉前面的字符串即可,Python中替换 \xef\xbb\xbf 即可