我是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