欢迎访问上海市某某有限公司
上海市某某有限公司专注网站制作10年!为企业打造专业的互联网营销解决方案
全国咨询热线: 18123456789
联系我们

上海市某某有限公司

地址:某某市某某区某某公路666弄66号

手机:18123456789

电话:021-6666666

邮箱:313801120@qq.com

当前位置:首页 > 新闻资讯 > 编程语言 > asp >

ASP接受JSON请求

时间:2022/7/27 10:40:12 作者: 点击:112次

ASP发送JSON请求

 
  
 <% 
 postUrl="http://xiyueta/1.asp" 
 postStr="{""title"":""php"",""b"":""mysql"",""c"":3}" 
  
     set http = createObject("Microsoft.XMLHTTP") 
         call http.open("POST", postUrl, false)  
         call http.setRequestHeader("cache-control", "no-cache")  
         call http.setRequestHeader("Content-Type", "application/json")  
         call http.setRequestHeader("Connection", "close")  
         call http.setRequestHeader("Content-Length", len(postStr))                      '可以不需要 
  
         call http.send(cStr(postStr))           '转成字符,为了在vb.net里可以用,晕,不知道为什么20161025  
         if http.readyState <> 4 then 
             content = "error"  
         else 
             content = bytesToBstr(http.responseBody, "utf-8")  
         'content = bytes2BSTR(Http.responseBody)    '这个要比上面那个好用   有时也不好用 
         end if  
  
  
  
         response.write(content) 
  
 function bytesToBstr(byteArr, cset) 
     dim objStream  
     if isNul(byteArr) then exit function                                               '为空则退出 
     set objStream = createObject("ADODB.Stream") 
         objStream.type = 1  
         objStream.mode = 3  
         objStream.open  
         call objStream.write(byteArr)  
         objStream.position = 0  
         objStream.type = 2  
         objStream.charset = cset  
         bytesToBstr = objStream.readText  
         objStream.close  
     set objStream = nothing  
 end function  
 '判断是否为空 
 function isNul(byVal s) 
     on error resume next : if err.number <> 0 then err.clear  
     isNul = false  
     select case varType(s) 
         case vbEmpty, vbNull 
             isNul = true : exit function  
         case vbString 
             if s = "" then isNul = true : exit function  
         case vbObject 
             select case typeName(s) 
                 case "Nothing", "Empty" 
                     isNul = true : exit function  
                 case "Recordset" 
                     if s.state = 0 then isNul = true : exit function  
                     if s.BOF and s.EOF then isNul = true : exit function  
                 case "Dictionary" 
                     if s.count = 0 then isNul = true : exit function  
             end select 
     case vbArray, 8194, 8204, 8209 
         if uBound(s) = -1 then isNul = true : exit function  
     end select  
     on error goto 0  
 end function  
 %> 
 
ASP接受JSON请求
<% 
 '字节流转为字符串 
 function bytes2bstr(vin) 
     dim bytesstream,stringreturn     
     set bytesstream = server.CreateObject("adodb.stream")    
     bytesstream.type = 2     
     bytesstream.open     
     bytesstream.writeText vin    
     bytesstream.position = 0     
     bytesstream.charset = "utf-8"'或者gb2312   
     bytesstream.position = 2     
     stringreturn = bytesstream.readtext  
     bytesstream.close    
     set bytesstream = nothing    
     bytes2bstr = stringreturn 
 end function 
  
 '解析json  
 Function parseJSON(str)   
     If Not IsObject(scriptCtrl) Then   
         Set scriptCtrl = Server.CreateObject("MSScriptControl.ScriptControl")   
         scriptCtrl.Language = "JScript"   
         scriptCtrl.AddCode "Array.prototype.get = function(x) { return this[x]; }; var result = null;"   
     End If   
     scriptCtrl.ExecuteStatement "result = " & str & ";"   
     Set parseJSON = scriptCtrl.CodeObject.result   
 End Function 
  
  
 '获取Post中的字节流大小 
 getpostjson=Request.TotalBytes 
  
 if getpostjson=0 then    
     response.Write("json null")  
     response.End() 
 end if 
  
 '读取POST所传递的字节流 
 readjson=Request.BinaryRead(getpostjson)  
  
 '将字节流转为字符串 
 json = bytes2bstr(readjson) 
 response.write(json) 
  
 set fso = createObject("Scripting.FileSystemObject")   
         set fText = fso.createTextFile(server.mapPath("1.txt"), true) 
             fText.writeLine(json)  
             createFile = true  
         set fText = nothing  
 set fso = nothing  
  
 '解析JSON 
 Set obj = parseJSON(json)  
     title=obj.title   '标题 
  
     set fso = createObject("Scripting.FileSystemObject")   
             set fText = fso.createTextFile(server.mapPath("2.txt"), true) 
                 fText.writeLine(title)  
                 createFile = true  
             set fText = nothing  
     set fso = nothing  
  
  
  
 Set obj = Nothing 
  
 %>