我有REST API服务。下面是API控制器方法。
public class ExcelUploadController : ApiController
{
[HttpPost]
public async Task<HttpResponseMessage> Upload(HttpRequestMessage request)
{
if (!request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
HttpPostedData postedData = await Request.Content.ParseMultipartAsync();
ProcessFile postedFile = new ProcessFile(postedData);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(postedFile.Process())
};
}
}
返回上传的excel的JSON数据。
下面是我使用RESTSharp的客户端代码
int ReadWriteTimeout = -1;
RestClient restClient = new RestClient("https://restfulservice.domain.com");
RestRequest restRequest = new RestRequest("api/excelupload/Upload");
int readWriteTimeout = restRequest.ReadWriteTimeout > 0
? restRequest.ReadWriteTimeout
: ReadWriteTimeout;
restRequest.ReadWriteTimeout = -1;
restClient.Timeout = -1;
restClient.ReadWriteTimeout = -1;
restRequest.Method = Method.POST;
restRequest.AddHeader("Content-Type", "multipart/form-data");
restRequest.AddFile("content", location);
restRequest.AddParameter("DetailLineNumber", "4");
var response = restClient.Execute<List<ActualsDataViewModel>>(restRequest);
我希望我的客户能够等到服务完成处理。但是客户端没有等待,响应对象为空表示状态码为0。response.Data为null。
如果我在本地主机中运行相同的REST API服务并替换客户端中的以下代码,则它可以正常工作。
RestClient restClient = new RestClient("http://localhost:5100");
如果服务器(IIS)上正在运行相同的服务,为什么客户端没有在等待响应?
答案 0 :(得分:0)
发布答案以节省其他人的时间。我知道了
我在调用REST API之前添加了以下内容。 @Hans Vonn在Unable to read data from the transport connection : An existing connection was forcibly closed by the remote host第二个答案中提出了这个答案。
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;