情况大概是这样的,我需要获取微信小程序的小程序码,微信官方提供接口了。于是我照着文档去请求。基本上大多写ASP的请求函数是类似这样的
BASIC
Function PostHttpPage(HttpUrl,PostData)
Dim Http
Set Http=server.createobject("WinHttp.WinHttpRequest.5.1")'Msxml2.ServerXMLHTTP
Http.open "POST",HttpUrl,False
Http.setRequestHeader "Content-Length",Len(PostData)
Http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
Http.Send PostData
PostHttpPage=bytesToBSTR(Http.responseBody,"utf-8")
Set Http=Nothing
End Function
Function BytesToBstr(Body,Cset)
Dim Objstream
Set Objstream = Server.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
请求其他接口倒是一直没问题,来到请求二维码这一块,返回了一堆乱码,一看就知道是二进制的文件流。
其实也简单,类似于输出验证码一样,输出流,定义一个文件头也就行了。
BASIC
Response.ContentType = "image/jpeg"
Response.BinaryWrite PostHttpPage("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="&get_xcx_access_token(),"{""scene"":""test"",""path"":""pages/index/index"",""width"":430,""auto_color"":false,""line_color"":{""r"":""0"",""g"":""0"",""b"":""0""}}")
Response.end
结果死活显示一个小框框,一直试着改文件头什么的,就是不行。
折腾了大半个小时,最后偶然喵到PostHttpPage=bytesToBSTR(Http.responseBody,”utf-8″) 这一行,恍然大悟,平时请求其他文本接口,把返回的内容转成utf-8就没问题,但是图片的二进制流是根本无需要转码的。于是再加一个function把转utf-8那部分去了
BASIC
Function PostHttpimg(HttpUrl,PostData)
Dim Http
Set Http=server.createobject("WinHttp.WinHttpRequest.5.1")'Msxml2.ServerXMLHTTP
Http.open "POST",HttpUrl,False
Http.setRequestHeader "Content-Length",Len(PostData)
Http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
Http.Send PostData
PostHttpimg=Http.responseBody
Set Http=Nothing
End Function
Response.ContentType = "image/jpeg"
Response.BinaryWrite PostHttpimg("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="&get_xcx_access_token(),"{""scene"":""test"",""path"":""pages/index/index"",""width"":430,""auto_color"":false,""line_color"":{""r"":""0"",""g"":""0"",""b"":""0""}}")
Response.end
果然OK了,之前做微信公众号接口的时候,在素材管理部分也有类似获取文件二进制流的事情,由于微信公众号那边接口比较科学,是先通过文本接口返回一个down_url字段再去获取的二进制流的,所以当时就没有掉坑里,来到小程序这里,他文本接口跟二进制流接口混一起了,确实容易害开发者掉坑里,但是为这问题浪费时间的主要责任还是平时自己编程习惯不好。算是大部分ASPER都容易犯的错吧。
评论前必须登录!
注册