Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1244|回复: 2

关于http请求返回结果错误的问题

[复制链接]
发表于 2021-10-19 16:25 | 显示全部楼层 |阅读模式
求教各位大神,因对http这部份不是很懂,从网上找的代码,自已也研究了半天,但还是没有成功,想请大定帮忙看一下。

问题:发送请求后,http.responseText返回的内容是错误的,查找了请求先关的代码,实在找不出原因。求指点。

  1. Private Sub 收件信息识别()

  2.     For i = 2 To Range("A1").End(xlDown).Row
  3.    
  4.         addaress = Range("a" & i).Value
  5.         
  6.         Url = "https://www.youbianku.cn/api/search_street_custom.php?address=" & addaress & "&key=MjAyMS02LTMweW91Ymlhbmt1c3RyZWV0youbianku"
  7.         
  8.         Dim http As Object
  9.    
  10.             Set http = CreateObject("Microsoft.XMLHTTP")     ' 创建 http 对象以发送请求
  11.             
  12.             http.Open "POST", Url, False                      ' 设置请求地址
  13.             
  14.             http.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"     '设置请求头
  15.         
  16.             http.send    '发送请求

  17.             
  18.             If http.Status = 200 Then
  19.                
  20.                Dim json                      '定义字符串 json
  21.                
  22.                json = http.responseText       '获取相应结果
  23.                
  24.                MsgBox json
  25.                
  26.                '接下来是解析 json
  27.             
  28.                strJSON = "var json=" & json
  29.                
  30.                objSC.AddCode (strJSON)        '将 json 由字符串解析为对象
  31.                
  32.                Dim geocodes
  33.                
  34.                geocodes = objSC.Eval("json.geocodes")
  35.                
  36.                If geocodes <> "" Then
  37.                
  38.                 Dim province$
  39.                
  40.                 province = objSC.Eval("json.geocodes[0].province")
  41.               
  42.                     If province <> "" Then
  43.                
  44.                         Cells(i, "C").Value = objSC.Eval("json.geocodes[0].province")   '将省填入 Excel 表格
  45.                         
  46.                         Cells(i, "D").Value = objSC.Eval("json.geocodes[0].city")   '将市填入 Excel 表格
  47.                         
  48.                         Cells(i, "E").Value = objSC.Eval("json.geocodes[0].district")   '将区填入 Excel 表格
  49.                         
  50.                     End If
  51.                
  52.                 End If
  53.                
  54.             End If
  55.     Next

  56. End Sub
复制代码


test.zip

12.33 KB, 下载次数: 1

 楼主| 发表于 2021-10-20 13:57 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2021-10-21 10:18 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 02:54 , Processed in 0.364970 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表