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))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 flask项目,怎么使用AJAX传数据库数据到echarts图表的data里,实现异步加载数据。
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题