首页 > Web开发 > 详细

深入浅出WebService之SOAP请求

时间:2020-06-17 16:45:12      阅读:68      评论:0      收藏:0      [点我收藏+]

官方示例

技术分享图片

 

 

 可以看到SOAP请求实际就是一个HTTP请求,当然,也有封装好的SOAP类库可以使用,那么实际呢就是发送一个XML文件流,SOAPAction键WebServices/HelloWorld定义为命名空间/函数

我们做一个最简单的WebService例子,接收一个string STR,直接放回STR

WebService服务端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Services;

namespace WebApplication1
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "WebServices")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld(string STR)
{
return $"{STR}";
}
}
}

 

 

 WebService客户端

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest httpWebRequest = WebRequest.Create("http://192.168.3.105/WebService1.asmx") as HttpWebRequest;
httpWebRequest.ContentType = "text/xml; charset=utf-8";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("SOAPAction", "WebServices/HelloWorld");
string STR = "请求的STR啊";
byte[] buff = Encoding.UTF8.GetBytes($"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><HelloWorld xmlns=\"WebServices\"><STR>{STR}</STR></HelloWorld></soap:Body></soap:Envelope>");
httpWebRequest.GetRequestStream().Write(buff, 0, buff.Length);
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(),Encoding.UTF8);
string TEXT = streamReader.ReadToEnd();
streamReader.Close();
httpWebResponse.Close();
}
}
}

TEXT为string放回值,放回值为

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><HelloWorldResponse xmlns="WebServices"><HelloWorldResult>请求的STR啊</HelloWorldResult></HelloWorldResponse></soap:Body></soap:Envelope>

那么拿到放回信息,我们可以用XML对象来查询,也可以用正则来提取我们想要的东西

就是这么一个简单的东西,不要想得太复杂了

深入浅出WebService之SOAP请求

原文:https://www.cnblogs.com/V-Sec/p/13153250.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!