3
我想在C#中執行HTTPS POST到沒有安裝正確的SSL證書(我的測試開發服務器)的服務器。該請求將使用WebClient以及HttpWebRequest超時。我已經建立了自己的ServerCertificateValidationCallback來繞過證書檢查,但這沒有幫助。如果我在網頁中進行完全相同的呼叫,則該呼叫成功。在C#HTTPS POST到服務器與證書不良 - 超時
所以:
我對Web客戶端後的代碼如下:
private static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
//solve the problem of invalid certificates - accept all as valid
return true;
}
public void callPost(object o)
{
string myData = (string)o;
try
{
Uri uri = new Uri("https://testServer/myAction");
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
byte[] uploadBytes = Encoding.UTF8.GetBytes(myData);
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
byte[] responseBytes = client.UploadData(uri, "POST", Encoding.UTF8.GetBytes(urlData));
Console.WriteLine("WebRequest:{0}\n", Encoding.ASCII.GetString(responseBytes));
}
catch (Exception e)
{
Console.WriteLine("WebRequest Error:{0}\n", e.ToString());
}
}
任何想法如何得到這個職位在C#中的工作?謝謝!
我不假設您可以將測試/ dev證書添加到您計算機的本地證書存儲中,以便它不再失效?或者,如果您在某個域上,請讓DC創建證書並將DC添加爲本地證書頒發機構? – Kye
@Kye是對的。我試過這一次,但我沒有在我們的服務器的適當的權限。 –