C# 如何在UnitTest类上创建HTTPS请求?
我创建了一个单元测试类来测试一个简单的HTTPS请求C# 如何在UnitTest类上创建HTTPS请求?,c#,visual-studio,unit-testing,https,C#,Visual Studio,Unit Testing,Https,我创建了一个单元测试类来测试一个简单的HTTPS请求 [TestMethod] public async Task TestMethod1() { string destination = "..."; string message = ".."; string accountUsername = ".."; string accountPassword = ".."; string senderPhoneNumber = "..."; Htt
[TestMethod]
public async Task TestMethod1()
{
string destination = "...";
string message = "..";
string accountUsername = "..";
string accountPassword = "..";
string senderPhoneNumber = "...";
HttpStatusCode response = await SMSProvider.Send(destination, message, accountUsername, accountPassword, senderPhoneNumber);
Assert.AreEqual(HttpStatusCode.GatewayTimeout, response);
}
SMS提供者是一个库类
public static async Task<HttpStatusCode> Send(String destination, String message, String accountUsername, String accountPassword, String senderPhoneNumber)
{
HttpClient client = new HttpClient();
var values = new Dictionary<string, string>
{
{ "username", accountUsername },
{ "password", accountPassword },
{ "src", senderPhoneNumber },
{ "dst", destination },
{ "text", message },
{ "srr", "3" }
};
var content = new FormUrlEncodedContent(values);
HttpStatusCode responseStatus = HttpStatusCode.Accepted;
try
{
var response = await client.PostAsync("https:SPECIFIC-URL-HERE", content);
var responseString = await response.Content.ReadAsStringAsync();
responseStatus = response.StatusCode;
}
catch (WebException we)
{
string error = we.ToString();
}
catch (Exception e)
{
string error = e.ToString();
}
return responseStatus;
}
如何发送HTTPS请求?
我需要证书吗?这有什么帮助吗?现在刚刚尝试过这种方法:D仍然不工作=(你检查过证书以确保它是可信的吗?什么证书?我对这个有点陌生。据我所知,服务器有一个证书,其中包含公钥(我认为是理论上的)。我需要安装什么吗?如果你想通过HTTPS与服务器通信,那么它必须安装一个受信任的证书,或者安装一个不受信任的证书,它们与你共享并在你的应用程序中使用。在浏览器中打开Url(使用HTTPS),在Url的左侧,你应该有一个安全的指示(通常是挂锁).先检查一下。
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.