C# https 連接相關問題.
/*忽略 SSL 有效性驗證.*/
void Application_Start(object sender, EventArgs e)
{
// 應用程式啟動時執行的程式碼
ServicePointManager
.ServerCertificateValidationCallback +=
(sender2, cert, chain, sslPolicyErrors) =>
{
if (sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}
var request = sender2 as HttpWebRequest;
if (request != null)
{
var result = request.RequestUri.Host == "xx.com.tw";
return result;
}
return false;
};
}
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
var uri = "https://www.google.com.tw";
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;
/*指定使用 TLS 1.2 做連接 */
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
req.Method = "GET";
req.UserAgent = context.Request.UserAgent;
req.ContentType = "application/x-www-form-urlencoded";
req.Proxy = new WebProxy("proxy.xx.com.tw", 8080);
string result = "";
// 取得回應資料
using (HttpWebResponse response = req.GetResponse() as HttpWebResponse)
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
result = sr.ReadToEnd();
}
}
context.Response.Write(result);
}