drg5577 2019-07-02 15:40
浏览 51
已采纳

如何将XML字符串发布到Web服务而不是XML文件

I have sucessfully posted an http request to a web service running on one of our servers.

Please note I did not write the web service and have no access to the source code.

Working code:

resp, err := http.Post("http://xxx.xxx.xx.xxx:8080/webservice/", "c:\\xml\\test.xml", nil)

I now instead of using an xml file need to build up the xml from a list of values.

I have written a function that does this, it basically pieces together the xml in a string builder. and my output string looks correct.

What I am not sure of is how do I post the xml as the post function seems to take the path to a file rather than a string?

Also is building it together as a string the correct method or should I approach it differently?

  • 写回答

1条回答 默认 最新

  • duanqin2026 2019-07-02 15:52
    关注

    I'm not sure where you took

    resp, err := http.Post("http://xxx.xxx.xx.xxx:8080/webservice/", "c:\\xml\\test.xml", nil)
    

    from. The docs say the signature of net/http.Post is

    func Post(url, contentType string, body io.Reader) (resp *Response, err error)
    

    That is, the second parameter is the content type of the payload to be sent, and the third is the source from which to read the data to be sent.

    Hence, if you have a string containing the XML data (building an XML document using string operations is almost always a wrong thing to do but let's not digress for now), you can have an io.Reader to read data from it by calling strings.NewReader() on your string.

    So the call would become

    resp, err := http.Post("http://xxx.xxx.xx.xxx:8080/webservice/", "application/xml", strings.NewReader(yourStringWithXML))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码