首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> ASP.NET 基礎

解決 IIS 7.5 上 .NET 無法獲取 HTTPS 頁面內容的問題

     編輯:ASP.NET 基礎

在 IIS 7.5 部署環境中,使用 HttpWebRequest 請求 HTTPS 頁面常因 SSL/TLS 協議版本、憑證驗證、Cookie 容器未正確配置而失敗。本文提供完整的 GET/POST 封裝方法,支援 Cookie 容器與 SSL3/TLS 協議指定。

核心設定要點

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; // 或 Tls / Tls11 / Tls12
// 生產環境建議:SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls

GET 請求封裝

public static string GetWebContent(string url, CookieContainer cookieContainer)
{
    string result = "";
    try
    {
        var request = (HttpWebRequest)WebRequest.Create(url);
        request.CookieContainer = cookieContainer;
        request.Timeout = 30000;
        request.Headers.Set("Pragma", "no-cache");
        request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1)";

        using (var response = (HttpWebResponse)request.GetResponse())
        using (var stream = response.GetResponseStream())
        using (var reader = new StreamReader(stream, Encoding.GetEncoding("utf-8")))
        {
            result = reader.ReadToEnd();
        }
    }
    catch (Exception ex)
    {
        // 記錄日誌 ex.Message
    }
    return result;
}

POST 請求封裝

public static string PostWebContent(string postUrl, string postData, CookieContainer cookieContainer, string contentType = "application/x-www-form-urlencoded", string referer = "", string method = "POST")
{
    string result = "";
    try
    {
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

        var request = (HttpWebRequest)WebRequest.Create(postUrl);
        request.CookieContainer = cookieContainer;
        request.Method = method;
        request.ContentType = contentType;
        request.Referer = referer;
        request.Timeout = 30000;
        request.KeepAlive = true;
        request.AllowAutoRedirect = false;

        byte[] postBytes = Encoding.UTF8.GetBytes(postData);
        request.ContentLength = postBytes.Length;

        using (var stream = request.GetRequestStream())
        {
            stream.Write(postBytes, 0, postBytes.Length);
        }

        using (var response = (HttpWebResponse)request.GetResponse())
        using (var stream = response.GetResponseStream())
        using (var reader = new StreamReader(stream, Encoding.UTF8))
        {
            result = reader.ReadToEnd();
        }
    }
    catch (Exception ex)
    {
        // 記錄日誌
    }
    return result;
}

常見問題排查

現象原因解決
基礎連接已關閉/信任關係失敗伺服器憑證不受信任(自簽、過期、CN 不符)開發環境可暫時忽略:ServicePointManager.ServerCertificateValidationCallback = (a,b,c,d) => true;。生產環境務必安裝有效憑證。
請求逾時防火牆攔截、伺服器無回應、SSL 協議不匹配檢查 443 埠通訊、嘗試指定 SecurityProtocolType.Tls12、增加 Timeout。
Cookie 不會自動帶入未指定 CookieContainer建立共用 CookieContainer 實例,傳給所有請求。
中文亂碼編碼不一致明確指定 Encoding.UTF8 或根據 response.CharacterSet 動態解碼。

進階建議

  • 使用 HttpClient ( .NET 4.5+ ) 替代 HttpWebRequest,支援非同步、連線池、更簡潔 API。
  • 啟用 HttpClientHandler 的 UseCookies = true 與 CookieContainer。
  • 生產環境強制 TLS 1.2+,停用 SSL3/TLS 1.0(PCI DSS 合規)。
  • 封裝為可重用服務類,統一處理重試、熔斷、日誌、監控。

正確配置協議版本、憑證驗證與 Cookie 容器,即可在 IIS 7.5 穩定獲取 HTTPS 頁面內容。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved