我需要从一个本地服务中消费生成的XML文档流。尽管终点不是REST服务。我想确保下面概述的方法是将响应返回到XDocument
中最有效的方式。
Uri requestUri = null;
Uri.TryCreate(String.Format(SearchAddress, filter),
UriKind.Absolute, out requestUri);
NetworkCredential nc =
new NetworkCredential("Login", "Password");
CredentialCache cCache = new CredentialCache();
cCache.Add(requestUri, "Basic", nc);
HttpWebRequest request =
(HttpWebRequest)HttpWebRequest.Create(requestUri);
request.Credentials = cCache;
request.PreAuthenticate = true;
request.Method = WebRequestMethods.Http.Get;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
XDocument xDoc =
XDocument.Load(new StreamReader(response.GetResponseStream()));
WebException
的想法不错。话说,对于异步请求,你建议的修改会是什么? - ahsteele