public class HttpClass { internal static HttpWebRequest _httpWebRequest; public static void Request() { _httpWebRequest = null; _httpWebRequest = (HttpWebRequest)WebRequest.Create("http://wcf.open.cnblogs.com/blog/sitehome/paged/1/2"); _httpWebRequest.Method = "GET"; _httpWebRequest.BeginGetResponse((result) => ResponseCallback(result), _httpWebRequest); } static object _obj = new object(); internal static void ResponseCallback(IAsyncResult result) { HttpWebRequest request = (HttpWebRequest)result.AsyncState; HttpWebResponse httpWebResponse; try { httpWebResponse = (HttpWebResponse)request.EndGetResponse(result); } catch (WebException we) { return; } if (httpWebResponse != null) { lock (_obj) { using (var stream = httpWebResponse.GetResponseStream()) { try { XmlSerializer ser = null; object obj = null; ser = new XmlSerializer(typeof(NewsFeed)); obj = ser.Deserialize(stream); } catch (Exception se) { return; } } } } } }
本文介绍了一个使用 C# 发起 HTTP GET 请求并解析返回 XML 数据的示例。该示例展示了如何创建 HttpWebRequest 对象,发起请求,并通过 BeginGetResponse 方法进行异步处理。同时,还展示了如何使用 XmlSerializer 解析响应流中的 XML 数据。
4

被折叠的 条评论
为什么被折叠?



