我是REST webservices的新手。我需要在C#控制台应用程序中使用现有的REST服务。我将在以下行中获得XML响应。
readStream.ReadLine();
我们如何在客户端中使用REST响应?
效用
public void SearchContactDetailsAsync(Models.AddressBookRequest addressBookDataRequest)
{
UriBuilder builder = new UriBuilder(url);
restClient.DoPost(builder.Uri, Serializer.SerializeXml(addressBookDataRequest.contactsSearchCriteria), SearchContactSuccess, SearchContactFailed, addressBookDataRequest.HeaderParams);
}
private void SearchContactSuccess(HttpWebResponse response)
{
//Call base service method - to inspect the response and publish an event
HandleServiceSearchSuccess<ContactDetailsPreview[]>(SearchContactDetailsCompleted, "contactDetailsPreviews", response);
Stream receiveStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.UTF8;
StreamReader readStream = new StreamReader(receiveStream, encode);
readStream.ReadLine();
}
控制台应用
public void MyMethod()
{
autoRestEvent = new AutoResetEvent(false);
services = new communicationSvcs();
services.SearchContactDetailsCompleted += new EventHandler<RestClientUtility.EventArg.ServiceResponseEventArgs<RestClientUtility.Models.ContactDetailsPreview[]>>(services_SearchContactDetailsCompleted);
//Call the operation
AddressBookRequest req = new AddressBookRequest
{
contactsSearchCriteria = new ContactsSearchCriteria
{
searchUserID = "ss23ed"
},
HeaderParams = new RestClientUtility.Requests.HttpHeaderParms
{
UserId = "ss23ed",
UserPrincipalName = " ss23ed@hotmail.com",
ContentType = "application/xml"
}
};
services.SearchContactDetailsAsync(req);
autoRestEvent.WaitOne();
}
参考
答案 0 :(得分:1)
.NET的XmlDocument Class有一个Load()方法接受一个流
在我看来,你只需要提供流
XmlDocument doc = XmlDocument.Load( readStream );
我无法确定它是否是一个静态方法,我没有在这里测试它的环境,但是你可能需要首先创建一个XmlDocument实例然后从它运行Load()方法(如果它不是静态)
答案 1 :(得分:1)
要从流中创建XmlDocument
: -
XmlDocument document = new XmlDocument();
using(StreamReader readStream = new StreamReader(receiveStream, encode))
{
document.Load(readStream);
}
重写MyMethod
以获取XmlDocument
:
public void MyMethod(XmlDocument xDoc)
并在SearchContactSuccess
方法内传递,假设您有一个类的实例,并且引用了consoleapp / utility所在的位置等等:
MyMethod(document);
答案 2 :(得分:1)
以下是用于从REST服务读取响应的伪代码。这有一个通用的反序列化方法
注意:ContactDetailsPreview是响应对象类型
//通用反序列化
public static T DeserializeXml<T>(Stream stream, XmlRootAttribute root)
{
XmlSerializer deserializer = new XmlSerializer(typeof(T), root);
return (T)deserializer.Deserialize(stream);
}
//代码段1
restClient.DoPost(builder.Uri, Serializer.SerializeXml(addressBookDataRequest.contactsSearchCriteria), SearchContactSuccess, SearchContactFailed, addressBookDataRequest.HeaderParams);
//代码段2
public event EventHandler<ServiceResponseEventArgs<ContactDetailsPreview[]>> SearchContactDetailsCompleted;
//代码段3
private void SearchContactSuccess(HttpWebResponse response)
{
//Call base service method - to inspect the response and publish an event
HandleServiceSearchSuccess<ContactDetailsPreview[]>(SearchContactDetailsCompleted, "contactDetailsPreviews", response);
Stream receiveStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.UTF8;
StreamReader readStream = new StreamReader(receiveStream, encode);
readStream.ReadLine();
}
//搜索成功响应的通用处理程序
public void HandleServiceSearchSuccess<T>(EventHandler<ServiceResponseEventArgs<T>> eventHandler, String rootElementName, HttpWebResponse response)
{
EventHandler<ServiceResponseEventArgs<T>> theGivenEventHandler = eventHandler;
obj = Serializer.DeserializeXml<T>(response.GetResponseStream(), new XmlRootAttribute() { ElementName = rootElementName });
theGivenEventHandler(this, new ServiceResponseEventArgs<T>(obj));
}