ASP模拟POST提交,然后XMLHTTP获取数据总是乱码,请大家帮忙,感谢!

目前在建的一个项目要求使用外部的一个网站达到切词的目的,由于外部网站的API接口要求必须是POST提交数据,因此只能模拟POST提交,然后再去读取提交后的数据,我用以下的代码,获取回来的中文总是乱码,英文和数字没有问题,请大家帮忙看看,非常感谢!

<%

On error resume next 

Function GetBody(ips) 

Set https = Server.CreateObject("MSXML2.XMLHTTP") 

With https 

.Open "Post", "http://www.ftphp.com/scws/api.php", False

.setRequestHeader "Content-Type","application/x-www-form-urlencoded;charset=utf-8"

.Send "data="&ips&"&respond=xml&charset=utf8&ignore=yes&duality=yes&traditional=no&multi=5"

GetBody = .ResponseBody

End With 

GetBody = BytesToBstr(GetBody,"utf-8")

Set https = 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

Response.Write GetBody("我们的李宇春天")

%>



是中文的且没有任何问题,
你发的代码,可以正确请求,并得到内容,且中文可以正常显示. ..


文件保存成UTF-8编码.(保存时可以用计事本另存为,编码选择UTF-8即可)
保存的UTF-8编码的文件,需要 BOM 头(IIS需要BOM头才能确定它是UTF-8编码的文件,我在XP下测试的,如果没有BOM头,则会显示ASP错误,因为IIS把它当成GB2312来解析了,然后那些UTF-8编码的汉字就会导致解析出错).


 






服务器交互时发送的HTTP交互的完整请求(对方返回的内容是UTF-8编码的内容,所以在ASP获取以后不需要转码,如果你需要以GB2312显示的时候才需要转)



  1. POST /scws/api.php HTTP/1.0
  2. Accept: */*
  3. Content-Type: application/x-www-form-urlencoded;charset=utf-8
  4. User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)
  5. Host: www.ftphp.com
  6. Content-Length: 97
  7. Connection: Keep-Alive
  8. Pragma: no-cache

  9. data=我们的李宇春天&respond=xml&charset=utf8&ignore=yes&duality=yes&traditional=no&multi=5
  10. HTTP/1.1 200 OK
  11. Server: nginx
  12. Date: Mon, 09 Apr 2012 06:12:12 GMT
  13. Content-Type: text/xml; charset=utf-8
  14. Connection: close
  15. Vary: Accept-Encoding

  16. <?xml version="1.0" encoding="utf-8"?>
  17. <scws:respond xmlns:scws="http://www.ftphp.com/scws">
  18.         <status>ok</status>
  19.         <words>
  20.                 <word><![CDATA[我们]]></word>
  21.                 <off>0</off>
  22.                 <len>6</len>
  23.                 <idf>4.4200000762939</idf>
  24.                 <attr>r</attr>
  25.         </words>
  26.         <words>
  27.                 <word><![CDATA[的]]></word>
  28.                 <off>6</off>
  29.                 <len>3</len>
  30.                 <idf>0</idf>
  31.                 <attr>uj</attr>
  32.         </words>
  33.         <words>
  34.                 <word><![CDATA[李宇春]]></word>
  35.                 <off>9</off>
  36.                 <len>9</len>
  37.                 <idf>9.3599996566772</idf>
  38.                 <attr>nr</attr>
  39.         </words>
  40.         <words>
  41.                 <word><![CDATA[天]]></word>
  42.                 <off>18</off>
  43.                 <len>3</len>
  44.                 <idf>0</idf>
  45.                 <attr>n</attr>
  46.         </words>
  47. </scws:respond>

复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值