在C#中使用SOAP并将响应发送回Rest [英] Consume SOAP and sending response back as Rest in C#

查看:88
本文介绍了在C#中使用SOAP并将响应发送回Rest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.net 4.5 C#项目.我正在使用SOAP服务,然后使用SharePoint 2013通过REST将其响应.

I am working with .net 4.5 C# project. I am consuming SOAP service and then response back them to with REST with SharePoint 2013.

但是问题是SOAP响应的一部分不是解析为XML,而是解析为字符串.我不知道我在做什么错.

屏幕标记的部分解析为字符串而不是XML

But the issue is part of SOAP response is not parsing as XML but as string. I don't know what I am doing wrong.

Screen marked part parsing as string not XML

消费SOAP代码

public HttpWebRequest CreateWebRequest(string URL)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"" + URL);
webRequest.Headers.Add(@"SOAP:Action");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}

public string ConsumeSOAP(string XML, string URL)
{
HttpWebRequest request = CreateWebRequest(URL);
XmlDocument soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.LoadXml(@"" + XML);

using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
using (WebResponse response = request.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd();
return soapResult;
}
}
}

SOAP对REST响应的代码

Code for SOAP response to REST

public string doLogin(string memberId, string password)
{
string XML = null;
try
{
SOAPService soap = new SOAPService();
XNamespace ns = @"http://schemas.xmlsoap.org/soap/envelope/";

string URL = URL;
string User = User;
string Password = Password;

string authenticateXML = string.Format(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:web=""http://webservices.xxx.xxx.com/"">
<soapenv:Header>
<web:header>
<passWord>{0}</passWord>
<userName>{1}</userName>
</web:header>
</soapenv:Header>
<soapenv:Body>
<web:authenticate>
<authenticateRequest>
<password>{2}</password>
<username>{3}</username>
</authenticateRequest>
</web:authenticate>
</soapenv:Body>
</soapenv:Envelope>", Password, User, password, memberId);

string authenticateResponse = soap.ConsumeSOAP(authenticateXML, URL);
var authenticateResponseValue = XDocument.Parse(authenticateResponse);
XML = authenticateResponseValue.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body").First().FirstNode.ToString();
}
catch 
{

}
return XML;
}

REST接口

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/doLogin?memberId={memberId}&password={password}")]
string doLogin(string memberId, string password);


[1]:https://i.stack.imgur.com/na54O.png


[1]: https://i.stack.imgur.com/na54O.png

推荐答案

以下代码供您参考:

using System.Xml;

namespace REST_XML_LIST_GET
{
	class Program
	{
		static XmlNamespaceManager xmlnspm = new XmlNamespaceManager(new NameTable());
		static Uri sharepointUrl = new Uri("Site URL/");
		static void Main(string[] args)
		{
			xmlnspm.AddNamespace("atom", "http://www.w3.org/2005/Atom");
			xmlnspm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
			xmlnspm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
			NetworkCredential cred = new System.Net.NetworkCredential("username", password", "domain");
			HttpWebRequest listRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "_api/Web/lists/getByTitle('List Name')/items");
			listRequest.Method = "GET";
			listRequest.Accept = "application/atom+xml";
			listRequest.ContentType = "application/atom+xml;type=entry";
			listRequest.Credentials = cred;
			HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse();
			StreamReader listReader = new StreamReader(listResponse.GetResponseStream());
			var listXml = new XmlDocument();
			listXml.LoadXml(listReader.ReadToEnd());


			//Method 1 Seperate node list
			var titleList = listXml.SelectNodes("//atom:entry/atom:content/m:properties/d:Title", xmlnspm);
			var idList = listXml.SelectNodes("//atom:entry/atom:content/m:properties/d:ID", xmlnspm);
			int i = 0;
			foreach (XmlNode title in titleList)
			{
				Console.WriteLine(title.InnerXml+" "+idList[i++].InnerXml);
			}


			//Method 2 single node list
			var prop = listXml.SelectNodes("//atom:entry/atom:content/m:properties", xmlnspm);
			foreach (XmlNode ndlist in prop)
			{
				Console.WriteLine(ndlist.SelectSingleNode("d:Title", xmlnspm).InnerXml + " " + ndlist.SelectSingleNode("d:ID", xmlnspm).InnerXml);
			}
			Console.ReadLine();

		}
	}
}

更多信息:

在C#托管代码中使用SharePoint 2013 REST API

http://www.c-sharpcorner.com/UploadFile/Roji.Joy/working-with-sharepoint-2013-rest-api-in-aC-Sharp-managed-code/

Sharepoint 2013 REST API:C#连接:使用System.Net.Http.HttpClient的第1部分

从C#逐步调用SharePoint 2013 REST API.

http://www.sp4geeks.com/2015/04/20/step-by-step-calling-sharepoint-2013-rest-api-from-c/

如果要使用C#在客户端访问SharePoint内容,我们还可以使用.NET客户端对象模型来实现它.

If you want to access SharePoint content in client side using C#, we can also use .NET client object Model to achieve it.

使用SharePoint 2013客户端库代码完成基本操作

https://msdn.microsoft.com/en-us/library/office/fp179912 .aspx

最好的问候,

丹尼斯


这篇关于在C#中使用SOAP并将响应发送回Rest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆