我是REST webservices的新手。我需要在C#控制台应用程序中使用现有的REST服务。我正在获取以下行中的XML响应。如何从客户端读取StreamReader响应?
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 = " [email protected]",
ContentType = "application/xml"
}
};
services.SearchContactDetailsAsync(req);
autoRestEvent.WaitOne();
}
参考
参考:http://stackoverflow.com/questions/14562415/xml-deserialization-generic-method – Lijo 2013-04-26 11:47:03