我有一个奇怪的问题,我不确定如何修复。目标是使用RestClient发布到API。这在Visual Studio中的调试模式下工作正常。但是,当我为Release构建项目并直接打开.exe时。 POST返回状态代码为0.
以下是我使用的一些代码片段。这还有点新意。谢谢你的帮助。
var url = "https://testurl.com";
//ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
// create a new RestSharp client
var client = new RestClient(url);
try
{
var request = new RestRequest(Method.POST)
{
RequestFormat = RestSharp.DataFormat.Json
};
request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
MessageBox.Show(url);
var response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.Created)
{
Console.WriteLine(JToken.Parse(response.Content));
ProcessResponse(response.Content);
}
else
{
MessageBox.Show("Request returned a status other than OK" + Environment.NewLine + "StatusCode: " + response.StatusCode.ToString() + Environment.NewLine + response.Content + Environment.NewLine + "The certificate used was " + cert.ToString(), "Error");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "There was a problem.");
}