为什么我的web api POST / PUT响应时间如此之慢,代码如下? POST的json内容只是< = 1024字节。
方法1和方法2我也试过但结果相同,慢......
已经尝试禁用代理;也很慢。
// GET API var testAsID = Convert.ToInt32(result); var getQueryString = string.Format("CoID={0}&ArID={1}", testCoID, testAsID); var response = await GetWebApiResponse(WebApiMethod.GET, controller, "GetByID", getQueryString); var result = await response.Content.ReadAsStringAsync(); // POST var newAS = new ASMtnModel() { CoID = "LKC3", AsID = 0, ClassID = "AR", AsCode = "AS Test 1", AsNameS = "AS Test 1", AsName1 = "AS Name 1", AsCurr = "RM", AsCat1 = "ZZZZZZ", AsProject = "ZZZZZZZZZZZZZZZ", AsCrTerm = "CASH", AsGLAcID = "", AsGLCcID = "", AsGLScID = "", AsCrLmt = 0, AsCrBal = 0, AsTaxType = "N", AsAccStatus = "1", AsStatus = "A", AsDateOpen = DateTime.Now, AsDateLSal = DateTime.Now, AsDateLPm = DateTime.Now, ApvDate = DateTime.Now, LastUpdateUser = "LLK", LastUpdateDate = DateTime.Now, AsGstDefaultValue = "E" }; var httpResponseMessage = await GetWebApiResponse(WebApiMethod.POST, controller, "Create", "", newAS); var result = await httpResponseMessage.Content.ReadAsStringAsync(); public async Task GetWebApiResponse(WebApiMethod webApiMethod, string controller, string action, string queryString = "", object requestBody = null) { var request = "/api/" + controller; if (!string.IsNullOrEmpty(action)) { request += "/" + action; } if (!string.IsNullOrEmpty(queryString)) { request += "?" + queryString; } var httpResponseMessage = new HttpResponseMessage(); if (webApiMethod == WebApiMethod.GET) { httpResponseMessage = await _client.GetAsync(request); } else if (webApiMethod == WebApiMethod.POST) { string postBody = JsonConvert.SerializeObject(requestBody); // Method 1 //httpResponseMessage = await _client.PostAsync(request, new StringContent(postBody, Encoding.UTF8, "application/json")); // Method 2 using (WebClient wc = new WebClient()) { wc.Proxy = null; var hostUrl = _client.BaseAddress.AbsoluteUri; if (hostUrl.EndsWith("/")) { hostUrl = hostUrl.Substring(0, hostUrl.Length - 1); } var fullApiUrl = hostUrl + request; byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postBody); wc.Headers[HttpRequestHeader.ContentType] = "application/json"; var resp = await wc.UploadDataTaskAsync(new Uri(fullApiUrl), bytes); httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK); httpResponseMessage.Content = new StringContent(System.Text.Encoding.UTF8.GetString(resp)); } } }
的Fiddler
请求数:1
发送的字节数:1,604(标题:506;正文:1,098)
收到的字节数:121(标题:120;正文:1)
实际表现
ClientConnected:10:07:42.186
ClientBeginRequest:10:11:33.569
GotRequestHeaders:10:11:33.569
ClientDoneRequest:10:11:33.569
确定网关:0ms
DNS查询:0ms
TCP / IP连接:0ms
HTTPS握手:0ms
ServerConnected:10:07:59.041
FiddlerBeginRequest:10:11:33.569
ServerGotRequest:10:11:33.569
ServerBeginResponse:10:12:21.874
GotResponseHeaders:10:12:21.874
ServerDoneResponse:10:12:21.875
ClientBeginResponse:10:12:21.874
ClientDoneResponse:10:12:21.875
Overall Elapsed: 0:00:48.306
响应字节(按内容类型)
〜头〜:120
application / json:1
服务器请求日志:
Executed action EB.WebApi.Controllers.ASMtnController.GetAll (EB.WebApi) in 282.741ms Request starting HTTP/1.1 POST http://localhost:5000/api/ASMtn/Create application/json 1098 dbug: Microsoft.AspNetCore.Routing.Tree.TreeRouter[1] Request successfully matched the route with name '' and template 'api/ASMtn/Create'. Request finished in 47928.9705ms 200 application/json; charset=utf-8