1

我是 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();

    }

参考

  1. XML反序列化泛型方法
4

3 回答 3

1

.NET 的XmlDocument 类有一个接受流的 Load() 方法

如我所见,您只需要向它提供流

 XmlDocument doc = XmlDocument.Load( readStream );

我真的看不出它是否是静态方法,并且我没有在这里测试它的环境,但也许您需要先创建 XmlDocument 的实例,然后从中运行 Load() 方法(如果它不是静态的)

于 2013-04-05T11:14:43.027 回答
1

从流创建一个XmlDocument: -

XmlDocument document = new XmlDocument();
using(StreamReader readStream = new StreamReader(receiveStream, encode)) 
{        
    document.Load(readStream);
}

重写MyMethod以获取XmlDocument

public void MyMethod(XmlDocument xDoc)

并在方法内部传递它SearchContactSuccess,假设您有一个类的实例和对控制台应用程序/实用程序所在位置的引用等:

MyMethod(document);
于 2013-04-05T11:24:22.453 回答
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));

}
于 2013-04-26T11:40:00.150 回答