我想用cURL发布XML数据.我不关心如何使用curl发布帖子请求中的表单.
我想使用cURL命令行界面将XML内容发布到某些Web服务.就像是:
curl -H "text/xml" -d "<XmlContainer xmlns='sads'..." http://myapiurl.com/service.svc/
Run Code Online (Sandbox Code Playgroud)
但是,上述样本无法由服务处理.
C#中的参考示例:
WebRequest req = HttpWebRequest.Create("http://myapiurl.com/service.svc/");
req.Method = "POST";
req.ContentType = "text/xml";
using(Stream s = req.GetRequestStream())
{
using (StreamWriter sw = new StreamWriter(s))
sw.Write(myXMLcontent);
}
using (Stream s = req.GetResponse().GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
MessageBox.Show(sr.ReadToEnd());
}
Run Code Online (Sandbox Code Playgroud)
Ben*_*mes 54
-H "text/xml"不是有效的标题.您需要提供完整的标题:
-H "Content-Type: text/xml"
Run Code Online (Sandbox Code Playgroud)
sto*_*333 13
我更喜欢以下内容:
cat req.xml | curl -X POST -H 'Content-type: text/xml' -d @- http://www.example.com
Run Code Online (Sandbox Code Playgroud)
要么
curl -X POST -H 'Content-type: text/xml' -d @req.xml http://www.example.com
Run Code Online (Sandbox Code Playgroud)
要么
curl -X POST -H 'Content-type: text/xml' -d '<XML>data</XML>' http://www.example.com
Run Code Online (Sandbox Code Playgroud)
使用req.xml您要发送的内容的文件(在我的情况下)更简单- 如下所示:
curl -H "Content-Type: text/xml" -d @req.xml -X POST http://localhost/asdf
你也应该考虑使用'application/xml'这个类型(这里解释的差异)
或者,不需要让curl实际读取文件,您可以使用cat将文件吐入stdout并使其curl从stdout读取,如下所示:
cat req.xml | curl -H "Content-Type: text/xml" -d @- -X POST http://localhost/asdf
两个示例都应该生成相同的服务输出
| 归档时间: |
|
| 查看次数: |
117102 次 |
| 最近记录: |