从C#直接将原始SOAP XML发送到WCF服务
我有一个WCF服务参考:
http://.../Service.svc(?WSDL)
我有一个包含兼容SOAP信封的XML文件
...
现在,我想通过一些C#代码将这些原始数据直接发送到服务(并接收响应),而不使用Visual Studio服务引用。
这是可能的,如果是的话,怎么样?
你可以使用UploadString 。 您需要适当地设置Content-Type
和SOAPAction
标头:
class Program { static void Main(string[] args) { using (var client = new WebClient()) { // read the raw SOAP request message from a file var data = File.ReadAllText("request.xml"); // the Content-Type needs to be set to XML client.Headers.Add("Content-Type", "text/xml;charset=utf-8"); // The SOAPAction header indicates which method you would like to invoke // and could be seen in the WSDL: element client.Headers.Add("SOAPAction", "\"http://www.example.com/services/ISomeOperationContract/GetContract\""); var response = client.UploadString("http://example.com/service.svc", data); Console.WriteLine(response); } } }
我只想评论Darin的回复对我有用,除了我必须取出SOAPAction头值附加的额外引号(当然替换你的uri):
client.Headers.Add("SOAPAction", "http://www.example.com/services/ISomeOperationContract/GetContract");
您可以尝试使用webclient类并将xml发布到服务。
- FIPSvalidation密码算法的问题
- 为什么不同的枚举类型之间的操作允许在另一个枚举声明中而在其他地方不允许?
- 有没有办法查看由JITter为给定的C#/ CIL生成的本机代码?
- 使用OpenXML SDK将RTF文件的内容嵌入到DOCX文件中
- 多对多的同一实体集合,具有双向关系
- Google Cloud Storage InsertMediaUpload Service BaseUri ArgumentNullException
- Powershell命令通过C#代码
- 在.Net 4.5中检查Active Directory组成员身份
- 默认(CancellationToken)如何具有相应的CancellationTokenSource