我正在Web API服务(Windows认证)中使用WebClient,以对我们的公司目录(Windows认证)进行HTTP调用,以获取用户的个人资料。请看下面。
public string DownloadPage(string ntid)
{
var result = "";
try
{
using (var client = new WebClient() {UseDefaultCredentials = true})
{
using (var stream = client.OpenRead($"{url}{urlParameter}{ntid}"))
{
using (var reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
}
}
catch (Exception ex)
{
log.Error(ex);
}
log.Info(result);
return result;
}
该代码在本地和我们的测试环境中均能完美运行,但是当我将其部署到Produciton环境时返回了“ 500内部服务器错误”。我根据500 internal server error at GetResponse()的解决方案解决了500个错误。请参阅下面的代码。但是,我发现只返回了html的一部分。是否需要通过代码或IIS设置主体长度或最大返回尺寸之类的东西?
public string DownloadPage(string ntid)
{
var result = "";
try
{
using (var client = new WebClient() {UseDefaultCredentials = true})
{
using (var stream = client.OpenRead($"{url}{urlParameter}{ntid}"))
{
using (var reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
}
}
catch (WebException webex)
{
//https://stackoverflow.com/questions/4098945/500-internal-server-error-at-getresponse
var errResp = webex.Response;
using (var stream = errResp.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
}
catch (Exception ex)
{
log.Error(ex);
}
log.Info(result);
return result;
}