Loading...

asp+vb.net解决调接口返回中文乱码问题

涉及语言:vb,vbscript,vb.net,asp

最近在工作中碰到了这样一个问题:需要调用一个接口解析简历文件中的关键信息。直接用postman测试该接口,接口返回值没问题,但一旦在asp里面调用就会出现中文乱码的问题。

 但界面接收到的返回值中文乱码

 数据库入库的中文也都是乱码

页面输出乱码很好解决的,只要把页面的输出编码设置为UTF-8,然后返回值设置成“二进制数据流数据”而不是“文本数据”。我用的语言是VB,VB调用接口是用WinHttp。

关于WinHttp的使用可以参考:WinHttp用法(WinHttp.WinHttpRequest.5.1方法,属性)_tylm22733367的博客-CSDN博客

至于入库中文乱码问题,那是由于WinHttp接口返回的winHttp.ResponseBody是一个二进制流,因此需要把该返回的二进制流转为UTF-8编码。

具体的转换代码可以参考(不过要注意最好手动敲下来,直接复制会有点问题的):

vb winhttp post 返回的数据 含有中文的部分是乱码,怎么解决?_百度知道 (baidu.com)

我的代码:

上传文件的前端ASP

<form method="post" id="frmQty" name="frmQty" action="myUpLoadFile.asp" enctype="multipart/form-data"> 		<label>文件名:</label> 		<input type="file" name="resume_file" id="resume_file"> 		<input type="submit" name="btn1" id="btn1" value="发布"> 	</form>

中间层处理ASP myUpLoadFile.asp

<% '设置页面的输出编码 Response.Charset="utf-8"   '相当于java中new一个对象 Set xxx = CreateObject("xxx.wsc")    if Request.TotalBytes > 0 then	  	filesize = Request.TotalBytes 	filedata = Request.BinaryRead(Request.TotalBytes)      contentType = Request.ServerVariables("CONTENT_TYPE")  	sTargetURL = "你要调用的接口地址" 	 	Set winHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")      winHttp.SetTimeouts 10000, 10000, 120000, 60000  	winHttp.Open "POST", sTargetURL, False	 	winHttp.setRequestHeader "Content-Type", contentType 	winHttp.setRequestHeader "Content-Length", filesize 	winHttp.setRequestHeader "Connection", "keep-alive"  	winHttp.Send filedata 	winHttp.WaitForResponse   	'Response.BinaryWrite winHttp.ResponseText 	Response.BinaryWrite winHttp.ResponseBody      '入库     Call xxx.AddHirePlan(6666,1,1,1,1,1,1,1,"testResume","sRequireSchool",1,"sRequireMajor","sRequireReason", 	xxx.BytesToBstr(winHttp.ResponseBody,"utf-8")) end If %>

后端编码转换方法:xxx.wsc中

Public Function BytesToBstr(Body,Cset) Dim Objstream Set Objstream = CreateObject("adodb.stream") Objstream.Type=1 Objstream.Mode=3 Objstream.Open Objstream.Write Body Objstream.Position=0 Objstream.Type=2 Objstream.Charset=Cset BytesToBstr=Objstream.ReadText Objstream.Close Set Objstream=Nothing End Function

WinHttp用法(WinHttp.WinHttpRequest.5.1方法,属性)_tylm22733367的博客-CSDN博客asp+vb.net解决调接口返回中文乱码问题https://blog.csdn.net/tylm22733367/article/details/52596990vb winhttp post 返回的数据 含有中文的部分是乱码,怎么解决?_百度知道 (baidu.com)asp+vb.net解决调接口返回中文乱码问题https://zhidao.baidu.com/question/519762516325259885.html