【发布时间】:2011-12-04 13:24:30
【问题描述】:
我正忙着将一个将音频文件发布到服务器的 perl 脚本转换为 C#
这是 perl 脚本:
#!/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;
这是我的 C# 代码
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);
由于某种原因,如果我运行 C# 代码,它会不断返回 400(错误请求)错误。
有人知道是什么原因造成的吗?提前致谢。
【问题讨论】:
-
尝试使用 Fiddler 等工具比较 Perl 脚本与 C# 代码完成的请求。 fiddler2.com/fiddler2 您可能遗漏了一些细微的差别。
-
我认为转义引号不应该在那里。试试
"Content-Type: audio/x-flac; rate=16000"。 -
使用提琴手我发现 myWebClient.UploadFile 导致 Content-Type 变为 multipart/form-data 所以我想我需要一些其他方式来上传文件
-
好的我解决了我的问题,我还不能发布答案,因为我的声誉不够高。