I’m busy converting a perl script which posts an audio file to a server to C#
this is the perl script:
#!/usr/bin/perl
require LWP::UserAgent;
my $url = "url";
my $audio = "";
open(FILE, "<" . "test.flac");
while(<FILE>)
{
$audio .= $_;
}
close(FILE);
my $ua = LWP::UserAgent->new;
my $response = $ua->post($url, Content_Type => "audio/x-flac; rate=16000", Content => $audio);
if ($response->is_success)
{
print $response->content;
}
1;
this is my C# code
string uriString = "url";
WebClient myWebClient = new WebClient();
myWebClient.Headers.Add("Content-Type: \"audio/x-flac; rate=16000\"");
byte[] responseArray = myWebClient.UploadFile(uriString, "POST", "test.flac");
string response = Encoding.ASCII.GetString(responseArray);
Console.WriteLine(response);
for some reason it keeps returning a 400 (bad request) error if I run the C# code.
Does anyone know what could be causing this? Thanks in advance.
Here are a couple ideas what the problem may be…
Use an alternate overload to add the header, this will help ensure it is formed correctly.
The paths of the source file name not be rooted the same… Try providing a fully qualified path to the file being uploaded. If that works, then you know where the issue is and do something more appropriate when building the paths.