i am trying to send a CSV File with http POST by means of the .NET framework. Having consulted various threads here, foremost “send rest-request with attached pfx certificate” i always end up with the receiving party answering with status code 401 “unauthorized”.
I have provided the receiving institution with the public part of an client certificate issued only for them. I exported the certificate from my local machine as .pfx file, including the private part.
In the first version of my program i apparently made the mistake to only attach the public part of the certificate. (Statement by the receiving party after asking them to check my incoming messages).
But now, having exported the certificate including the private part, i dont understand anymore what the problem is.
The URL which i am sending i checked with the receiving party, it is correct.
Here´s my main program:
handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.SslProtocols = SslProtocols.Tls12;
handler.ClientCertificates.Add(new X509Certificate(_cnf_CertificateLocalPath, "thepassword"));
fs = new FileStream(_cnf_FileToTransfer, FileMode.Open);
sr = new StreamReader(fs, Encoding.Default);
fileContent = sr.ReadToEnd();
sr.Dispose();
fs.Dispose();
Task mytask = Upload(handler, fileContent, "myfile.csv");
System.Threading.Thread.Sleep(5000);
currentTimestamp = DateTime.Now.Year.ToString() + "_" +
DateTime.Now.Month.ToString().PadLeft(2, '0') + "_" +
DateTime.Now.Day.ToString().PadLeft(2, '0') + "_" +
DateTime.Now.Hour.ToString().PadLeft(2, '0') + "_" +
DateTime.Now.Minute.ToString().PadLeft(2, '0') + "_" +
DateTime.Now.Second.ToString().PadLeft(2, '0') + "_" +
DateTime.Now.Millisecond.ToString().PadLeft(3, '0');
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("end of program run at {0}", currentTimestamp);
//Console.WriteLine("==> end of program run");
Console.WriteLine("==> press any key to close window");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
And here the Upload Method itself:
public static async Task Upload(HttpClientHandler handler, string parmInFileContent, string fileName)
{
using (var client = new HttpClient(handler))
{
using (var content =
new StringContent(parmInFileContent, Encoding.UTF8, "text/csv"))
{
using (
var message =
await client.PostAsync(_API_URL, content))
{
if (message.IsSuccessStatusCode)
{
Console.ForegroundColor = ConsoleColor.Green;
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
Console.WriteLine(message.Content.ToString());
Console.WriteLine(message.StatusCode.ToString());
Console.WriteLine(message.ToString());
Console.WriteLine(message.Headers.Warning.ToString());
}
}
}
}
I suspect that i am doing something wrong when generating the .pfx file. Regarding the program structure itself there seems not to be much of a choice, but i am not sure.
Any help would be appreciated.