我有一個Web API,我可以通過瀏覽器成功訪問: -C#POST請求的WebAPI使用控制檯程序
我試圖創建在C#中使用VS2015到一個簡單的控制檯程序發送數據並使用http POST接收響應。
這是我到目前爲止有: -
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace WebSample
{
class ApiSendData
{
public string ApiFunction { get; set; }
public string DppName { get; set; }
public string ClearData { get; set; }
public string DppVersion { get; set; }
}
class Program
{
static void Main(string[] args)
{
// The Main function calls an async method named RunAsync
// and then blocks until RunAsyncc completes.
RunAsync().Wait();
}
static async Task RunAsync()
{
using (var client = new HttpClient())
{
// This code sets the base URI for HTTP requests,
// and sets the Accept header to "application/json",
// which tells the server to send data in JSON format.
client.BaseAddress = new Uri("https://localhost:8443/ncrApi");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP POST
var datatobeSent = new ApiSendData()
{
ApiFunction ="NcrSecureData",
DppName ="CSampleCustomer",
DppVersion ="Latest",
ClearData ="1234567890"
};
HttpResponseMessage response = await client.PostAsJsonAsync("ncrApi", datatobeSent);
if (response.IsSuccessStatusCode)
{
// Get the URI of the created resource.
Uri ncrUrl = response.Headers.Location;
// do whatever you need to do here with the returned data //
}
}
}
}
}
但是我對HttpResonseMessage響應說法得到一個錯誤.... {「發送請求時發生錯誤。」} { 」請求被中止:無法創建SSL/TLS安全通道。「}
我懷疑這是因爲我沒有正確理解client.BaseAddress和HttpResponseMessage響應語句。
這是我一直在關注: - http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client
附加註釋: - 1)我關閉SSL,併成功連接到端口8080上的web api。 2)我成功地通過我的代碼連接到其他SSL api。 似乎C#代碼沒有連接到SSL連接ncrApi ..任何想法爲什麼? – Philo