在https的链接中vb使用post提交xml数据

在https的链接中vb使用post提交xml数据,第1张

概述  由于工作需要,需要vb提交xml数据到服务器上,所以,需要找到一个好的方法。   之前有代码使用了MSXML2.XMLHTTP对象进行xml数据取得,而且链接是https形式的,于是就参照代码提交,写了下面这么一个函数,来进行 *** 作 1 Private Function CallAPI(url1 As String, filePath As String) 2 'load file

  由于工作需要,需要vb提交xml数据到服务器上,所以,需要找到一个好的方法。

  之前有代码使用了MSXML2.XMLhttp对象进行xml数据取得,而且链接是https形式的,于是就参照代码提交,写了下面这么一个函数,来进行 *** 作

 1 Private Function CallAPI(url1 As String,filePath String) 2     'load file 3     Dim document As MSXML2.DOMdocument 4     Set document = CreateObject("MSXML2.DOMdocument") 5     document.Load filePath  load xml file 6     send data to server 7     Dim xmlhttp As MSXML2.XMLhttp 8     Set xmlhttp = MSXML2.XMLhttp 9     xmlhttp.Open POST",url1,False post method to10     xmlhttp.setRequestheader Content-Typeapplication/xml" xml type of http request xml,not text/xml11     xmlhttp.send (document.xml)12     clear13     nothing14     15 End Function

  但是,提交总是失败,在下面的这句代码就被中断,一直找不到原因。

1 xmlhttp.send (document.xml)

  突然想到http协议和https协议是不同的,可能在提交数据时有差异,猜测MSXML2.XMLhttp对象应该不支持https,经过寻找找到另一个对象Winhttp.WinhttpRequest,于是有了下面的代码

5 document.Load filePath 6 Dim xmlhttps As Winhttp.WinhttpRequestSet xmlhttps = Winhttp.WinhttpRequest.5.1 9 xmlhttps.Open False10 xmlhttps.setRequestheader "11 12 xmlhttps.send document.xml13 14 DoEvents15 16   执行了上面的代码之后程序正常结束,但是远端的服务器中的数据没有变化,查看了xmlhttps.status之后发现,提示没有权限,嗯,服务器那边需要用户名和密码验证的,这边没有设置登录用户名和密码,于是加了一句

11
xmlhttps.SetCredentials 用户名 密码",httpREQUEST_SETCREDENTIALS_FOR_SERVER12 13 xmlhttps.send document.xml14 15 DoEvents16 17   执行了代码之后发现,xml对象的数据被正常提交。

总结:

  1.MSXML2.XMLhttp对象不能提交https数据到服务器上

  2.Winhttp.WinhttpRequest对象需要显式的设置用户名和密码,在MSXML2.XMLhttp对象使用的过程中,没有登录的话,会d出用户名和密码输入框,而Winhttp.WinhttpRequest对象没有,直接提交数据后,提示没有权限

  3. 提交xml对象时,设置的请求头部应该是"Content-Type","application/xml"而不是"Content-Type","text/xml"

其它对象也可以达到https提交的效果:

1
... 2 Dim serverxml As MSXML2.ServerXMLhttp3 Set serverxml = MSXML2.ServerXMLhttp4 serverxml.Open False,0); Font-family:'CourIEr New'!important; line-height:1.5!important">用户名5 serverxml.setRequestheader 6 serverxml.send document.xml7 ...

  注:Winhttp.WinhttpRequest对象需要添加Microsoft Winhttp Services,version 5.1组件的引用

    MSXML2.XMLhttp和MSXML2.ServerXMLhttp需要添加Microsoft MsXml组件的引用

总结

以上是内存溢出为你收集整理的在https的链接中vb使用post提交xml数据全部内容,希望文章能够帮你解决在https的链接中vb使用post提交xml数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1267859.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存