First post…..
I am trying to upload data to a web site using C# and HttpClient.
The site responds when I use curl: curl -X POST https://www.eQSL.cc/qslcard/ImportADIF.cfm -F “Filename=@”D:DocumentsSourceCodeRadiobinDebugnet8.0-windowslogfile.adi”
etc…..
My C# code returns a status code “OK”, but the response is an empty string.
`using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
//https://www.eqsl.cc/QSLcard/ImportADIF.txt HOW TO USE THE “ImportADIF.cfm” PROGRAM for eQSL.cc
//https://www.eqsl.cc/QSLCard/ADIFContentSpecs.cfm
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
HttpClient client = new HttpClient();
string response = UploadLog(client).Result;
}
/*
curl -v POST https://www.eQSL.cc/qslcard/ImportADIF.cfm -F "Filename=@"D:DocumentsSourceCodeRadiobinDebugnet8.0-windowslogfile.adi"
returns a Web response
*/
private static async Task<string> UploadLog(HttpClient client)
{
string url = @"https://www.eQSL.cc/qslcard/ImportADIF.cfm";
string filePath = @"D:DocumentsSourceCodeRadiobinDebugnet8.0-windowslogfile.adi";
byte[] bytes = System.IO.File.ReadAllBytes(filePath);
MultipartFormDataContent formContent = new MultipartFormDataContent();
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamContent fileContent = new StreamContent(fileStream);
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
{
Name = "Filename",
FileName = System.IO.Path.GetFileName(filePath)
};
formContent.Add(fileContent);
formContent.Add(new StringContent("myUserID"), "EQSL_USER");
formContent.Add(new StringContent("myPass"), "EQSL_PSWD");
var response = await client.PostAsync(url, formContent);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
}
`
ChrisBrown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.