我在request.GetResponse().GetResponseStream()
上收到此警告我该如何处理?
public void GetUserList(string url)
{
var request = (HttpWebRequest) WebRequest.Create(url);
string response;
using (var stream = new StreamReader(request.GetResponse().GetResponseStream()))
{
response = stream.ReadToEnd();
}
response = DelimiterStrings.Aggregate(response, (current, delim) => current.Replace(delim, "\n"));
foreach (var line in response.Split(DelimiterChars))
{
MainWindow.UserList.Add(line);
}
}
Resharper不知道如何“修复它”
答案 0 :(得分:5)
与所述评论一样,首先分配响应流并将其检查为null,如下所示:
public void GetUserList(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
var responseStream = request.GetResponse().GetResponseStream();
if (responseStream != null)
{
string response;
using (var stream = new StreamReader(responseStream))
{
response = stream.ReadToEnd();
}
response = DelimiterStrings.Aggregate(response, (current, delim) => current.Replace(delim, "\n"));
foreach (var line in response.Split(DelimiterChars))
{
MainWindow.UserList.Add(line);
}
}
}
答案 1 :(得分:1)
将其分开:
var sourceStream = request.GetResponse().GetResponseStream();
Contract.Assume(sourceStream != null); // Let Resharper know it can't be null.
using (var stream = new StreamReader(sourceStream))
这样做的好处是记录和检查你的假设!