我是 REST 网络服务的新手。我需要在 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();
}
参考