我正在尝试根据this example - Method A在UWP C#应用中发出POST请求。 我的例子的代码是:
string scriptname = "myscript.php";
var content = new FormUrlEncodedContent(values);
//Exception Line (103):
var response = await client.PostAsync("https://myserver.ddns.net/" + scriptname, content);
var responseString = await response.Content.ReadAsStringAsync();
string SJson = responseString.ToString();
messagedialog.Content = SJson;
异常日志:
System.Net.Http.HttpRequestException
HResult = 0x80072F0D
消息=发送请求时发生错误 来源= System.Net.Http
堆栈跟踪: 在System.Net.Http.HttpClientHandler.d__86.MoveNext() 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在System.Net.Http.HttpClient.d__58.MoveNext() 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在System.Runtime.CompilerServices.TaskAwaiter1.GetResult() 在D:\ Onedrive \ myproject \ myproject \ App \ App \ MainPage.xaml.cs中的Aplikacija_iFE.MainPage.d__10.MoveNext():第103行
内部例外1: COMException:找不到与此错误代码关联的文本。
Overitelj digitalnih potrdil ni veljaven ali pajenapačen
粗体字符串是我的母语,告诉我CA无效或错误(基本上是因为我自己签名)。可以使用某些C#代码暂时修复此错误,还是必须更换证书?
我的HTTPS(Apache)服务器在Debian 9机器上。
编辑(晚上10:20):工作代码
下面的代码现在可以使用了,但是对于我来说,作为一个刚接触编程的学生,它是丑陋,高度不安全,对我来说只是一个阴影:
string scriptname = "MyRestAPI.php";
HttpFormUrlEncodedContent content = new HttpFormUrlEncodedContent(values);
HttpResponseMessage response = new HttpResponseMessage();
try
{
client = new HttpClient();
response = await client.PostAsync(new Uri("https://myserver.ddns.net/" + scriptname), content);
}
catch (Exception e)
{
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
ChainValidationResult[] results = new ChainValidationResult []
{
ChainValidationResult.Untrusted, ChainValidationResult.WrongUsage,
ChainValidationResult.BasicConstraintsError, ChainValidationResult.Expired,
ChainValidationResult.IncompleteChain, ChainValidationResult.InvalidCertificateAuthorityPolicy,
ChainValidationResult.InvalidName, ChainValidationResult.OtherErrors,
ChainValidationResult.RevocationFailure, ChainValidationResult.RevocationInformationMissing,
ChainValidationResult.Revoked, ChainValidationResult.UnknownCriticalExtension
};
for(int i=0;i<results.Length;i++)
{
try
{
filter.IgnorableServerCertificateErrors.Add(results[i]);
client = new HttpClient(filter);
response = await client.PostAsync(new Uri("https://myserver.ddns.net/" + scriptname), content);
}
catch
{
continue;
}
}
client = new HttpClient(filter);
response = await client.PostAsync(new Uri("https://myserver.ddns.net/" + scriptname), content);
}
finally
{
client.Dispose();
}
messagedialog.Content = response.Content.ToString();
答案 0 :(得分:1)
您可以使用配置在开发环境中忽略此错误,或让您的客户端信任该证书,即只需将证书添加到客户端上的受信任根目录。