簡體   English   中英

WebException 響應未返回完整響應

[英]WebException response didn't return the full response

我在我的 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 內部服務器錯誤”。 我根據GetResponse() 處 500 內部服務器錯誤的解決方案解決了 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;
    }

我認為您可以訪問活動目錄,這就是為什么代碼可以在本地運行,但在 IIS 上,應用程序池從沒有訪問權限的 ApplicationPoolIdentity 運行。

轉到應用程序池並將身份更改為您的帳戶。 這是一張圖片供您參考: 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM