c# - WebException 响应未返回完整响应
问题描述
我在我的 Web API 服务(Windows Autheticated)中使用 WebClient 对我们的公司目录(Windows Autheticated)进行 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;
}
该代码在本地和我们的测试环境中完美运行,但在我将其部署到生产环境时返回“500 内部服务器错误”。我根据GetResponse()的 500 internal server error 的解决方案解决了 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;
}
解决方案
推荐阅读
- powershell - 公用文件夹报告脚本 - Exchange 2010
- c# - Minio:放置带有附加数据的对象
- php - 自定义表格不起作用
- vba - 运行自动滚动宏,但有两个问题
- ios - 带有渐变层的 TableViewCell -> 布局问题
- momentjs - moment.utc - 无效输入字符串格式警告
- javascript - 如何使用来自 reducer 的一些数据更新组件的状态
- jquery - 如何将类添加到下一个 li 元素并将其从其他元素中删除?
- ssl - OpenSSL - 在本地系统中工作,但不在其他 LAN 系统中工作 [Intranet]
- magento - 添加到购物车无法使用 magento 版本 2.2.4