如何从WCF服务获取数据? [英] How to get a data from the WCF service?
本文介绍了如何从WCF服务获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,
i有一个WCF服务:
http://team.abc.net/site/DSP/培训/ _bin / LinkData.svc
来自这项服务我必须得到数据。我使用了以下代码,但它的抛出错误为未授权。
我也不知道如何通过uri对象传递用户名和密码,我试过uri建设者班也。但同样的问题。
请做必要的事情。
我尝试了什么:
1st我在解决方案中将服务引用添加为'TrainingLinkService'。
For Example,
i have a WCF service as:
http://team.abc.net/site/DSP/Training/_bin/LinkData.svc
from this service i have to get the data. i used the following code, but its throwing error as 'unauthorized'.
Also im not sure how to pass the username and password with the uri object, i tried with uri builder class also. but same problem.
Please do the needful.
What I have tried:
1st i added the service reference in solution as 'TrainingLinkService'.
var uri = new Uri("http://team.abc.net/site/DSP/Training/_bin/LinkData.svc");
TrainingLinkService.TrainingProgramReviewDataContext client = new TrainingLinkService.TrainingProgramReviewDataContext(uri);
var temp = from item in container.Documentation
select item;
推荐答案
以下是解决方案:它是Atom格式,所以我有点困惑解析它。
Here is the solution: it was in Atom format, so i was bit confused to parse it.
static List<DocumentationEntity> ParseAtom(string url, string username, string password)
{
try
{
var _xDocument = new XDocument();
var _Request = WebRequest.Create(url) as HttpWebRequest;
_Request.Method = "GET";
_Request.Credentials = new NetworkCredential(username, password);
using (HttpWebResponse response = _Request.GetResponse() as HttpWebResponse)
{
using (Stream stream = response.GetResponseStream())
{
_xDocument = XDocument.Load(stream);
}
}
var entries = from root in _xDocument.Root.Elements().Where(i => i.Name.LocalName == "entry")
from entry in root.Elements().Where(i => i.Name.LocalName == "properties")
select new DocumentationEntity
{
ContentTypeID = entry.Elements().First(i => i.Name.LocalName == "ContentTypeID").Value,
Name = entry.Elements().First(i => i.Name.LocalName == "Name").Value,
Description = entry.Elements().First(i => i.Name.LocalName == "Description").Value,
PictureWidth = entry.Elements().First(i => i.Name.LocalName == "PictureWidth").Value,
StageValue = entry.Elements().First(i => i.Name.LocalName == "StageValue").Value,
ScriptReview = entry.Elements().First(i => i.Name.LocalName == "ScriptReview").Value,
ScriptReview0 = entry.Elements().First(i => i.Name.LocalName == "ScriptReview0").Value,
ScriptCompleted = entry.Elements().First(i => i.Name.LocalName == "ScriptCompleted").Value,
Id = entry.Elements().First(i => i.Name.LocalName == "Id").Value,
ContentType = entry.Elements().First(i => i.Name.LocalName == "ContentType").Value,
Created = entry.Elements().First(i => i.Name.LocalName == "Created").Value,
CreatedById = entry.Elements().First(i => i.Name.LocalName == "CreatedById").Value,
Modified = entry.Elements().First(i => i.Name.LocalName == "Modified").Value,
ModifiedById = entry.Elements().First(i => i.Name.LocalName == "ModifiedById").Value,
CopySource = entry.Elements().First(i => i.Name.LocalName == "CopySource").Value,
ApprovalStatus = entry.Elements().First(i => i.Name.LocalName == "ApprovalStatus").Value,
Path = entry.Elements().First(i => i.Name.LocalName == "Path").Value,
CheckedOutToId = entry.Elements().First(i => i.Name.LocalName == "CheckedOutToId").Value,
VirusStatus = entry.Elements().First(i => i.Name.LocalName == "VirusStatus").Value,
IsCurrentVersion = entry.Elements().First(i => i.Name.LocalName == "IsCurrentVersion").Value,
Owshiddenversion = entry.Elements().First(i => i.Name.LocalName == "Owshiddenversion").Value,
Version = entry.Elements().First(i => i.Name.LocalName == "Version").Value
};
return entries.ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw ex;
}
}
这篇关于如何从WCF服务获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文