【发布时间】:2018-06-08 09:01:34
【问题描述】:
我正在从 .Net 3.5 Framework 客户端调用 2 个 Web 服务(服务器是更现代的 .NET Framework Server:4.6)。
分离后我没有遇到任何问题,但是如果我按照下面显示的顺序调用方法,我会遇到从未输入服务器上的 VerifyFile 方法的问题,而是立即得到一个 服务器在客户端上提交了协议 Violation Section=ResponseStatusLine 错误。 更准确地说:服务器在事件中注册了 VerifyFile 请求,但直到 6 分钟后才输入实际代码(并立即返回导致上述错误的内容)。
经过大量测试,我可以将其简化为第一种方法“DownloadFile”是问题的原因。并且总是当我从它返回状态码以外的任何其他内容时(内容与否都无关紧要)。
我对这种现象完全不知所措(我原以为客户端会遇到麻烦,但是服务器直到几分钟后才输入那个代码部分看起来服务器本身也遇到了麻烦,这出乎意料,同样出人意料的是第二个方法而不是原来的方法在那里遇到了问题)。
所以我的问题是为什么返回除了 HttpStatusCode.OK 之外的任何内容都会导致这些问题,我可以做些什么来纠正这个问题?
客户:
WebClient webClient = new WebClient();
webClient.QueryString.Add("downloadId", id);
webClient.DownloadFile("localhost/Download/DownloadFile", @"c:\temp\local.txt");
webClient = new WebClient();
webClient.QueryString.Add("downloadId", id);
webClient.QueryString.Add("fileLength", GetFileLength(@"c:\temp\local.txt"));
var i = webClient.DownloadString("localhost/Download/VerifyFile");
Testwise 我将下载文件替换为:webClient.DownloadString("localhost/Download/DownloadFile");
最初我也只有一个新的 WebClient,但在第一次失败后添加了第二个。
服务器:
[RoutePrefix("Download")]
public class DownloadController : ApiController
{
[HttpGet]
[Route("DownloadFile")]
public IHttpActionResult DownloadFile(int downloadId){
return ResponseMessage(GetFile(downloadId));
}
private HttpResponseMessage GetFile(int downloadId){
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
string filePath = GetFilePathFromDB(downloadid);
if (filePath != String.Empty){
var stream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(filePath);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;
}
else{
result = new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
return result;
}
[HttpGet]
[Route("VerifyFile")]
public IHttpActionResult VerifyFile(int downloadId, int fileLength)
{
return Content(HttpStatusCode.OK, "OK");
}
private string GetFilePathFromDB(int downloadId)
{
return @"C:\temp\mytest.txt"; // testcode
}
}
【问题讨论】:
-
@Thomas 我怀疑第二个操作是在上一次调用可用之前尝试访问文件流。
-
@Nkosi 我今天发现了发生了什么以及如何纠正它,但老实说,我无法找出原因。当我用 return Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Some message here"); 替换错误返回时问题停止了,一切都按预期工作。
-
@Thomas Not Found 也会发生同样的情况吗?
-
@Thomas,您正在创建一个没有内容且没有内容类型的响应,这可能是这里的问题吗?