当前位置:  开发笔记 > 运维 > 正文

如何从.NET发布SOAP请求?

如何解决《如何从.NET发布SOAP请求?》经验,为你挑选了2个好方法。

我在XML文件中有SOAP请求.我想将请求发布到.net中的Web服务如何实现?



1> 小智..:
var uri = new Uri("http://localhost/SOAP/SOAPSMS.asmx/add");

var req = (HttpWebRequest) WebRequest.CreateDefault(uri); 
req.ContentType = "text/xml; charset=utf-8"; 
req.Method = "POST"; 
req.Accept = "text/xml"; 
req.Headers.Add("SOAPAction", "http://localhost/SOAP/SOAPSMS.asmx/add"); 

var strSoapMessage = @"

  235
"; 

using (var stream = new StreamWriter(req.GetRequestStream(), Encoding.UTF8)) 
    stream.Write(strSoapMessage); 



2> marcus.greas..:

我做过类似的事情,手动构建一个xml请求,然后使用webrequest对象提交请求:

string data = "the xml document to submit";
string url = "the webservice url";
string response = "the response from the server";

// build request objects to pass the data/xml to the server
byte[] buffer = Encoding.ASCII.GetBytes(data);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = buffer.Length;
Stream post = request.GetRequestStream();

// post data and close connection
post.Write(buffer, 0, buffer.Length);
post.Close();

// build response object
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responsedata = response.GetResponseStream();
StreamReader responsereader = new StreamReader(responsedata);
response = responsereader.ReadToEnd();

代码开头的字符串变量就是你设置的,然后你从服务器得到一个字符串响应(希望......).

推荐阅读
雨天是最美
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有