如何从客户端读取StreamReader响应

本文关键字:StreamReader 响应 读取 客户端 | 更新日期: 2023-09-27 18:07:28

我是REST web服务的新手。我需要在c#控制台应用程序中使用一个现有的REST服务。我在下一行中得到了XML响应。

readStream.ReadLine();

我们如何在客户端使用REST响应?

<

效用/strong>

  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反序列化通用方法

如何从客户端读取StreamReader响应

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

在我看来,您只需要向它提供流

 XmlDocument doc = XmlDocument.Load( readStream );

我不能真正看到它是否是一个静态方法,我没有环境来测试它在这里,但也许你需要先创建一个XmlDocument的实例,然后从它运行Load()方法(如果它不是静态的)

从流创建一个XmlDocument: -

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

重写MyMethodXmlDocument:

public void MyMethod(XmlDocument xDoc)

并将其传递进来,在SearchContactSuccess方法中,假设您有类的实例和对控制台应用程序/实用程序所在位置的引用等:

MyMethod(document);

下面是读取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));
}