HttpClient PostAsync C#始终在使用Web API上返回404 Not Found

时间:2018-06-29 14:10:49

标签: c# node.js rest express httpclient

我在带有express的nodejs上有一个服务器代码,该服务在Postman的测试中效果很好

服务器代码:

app.post("/v2/addComment", (request, response) => {
   let n = parseInt(request.query.number);
   let key = request.get("key");
   let token = request.get("token");
   getToken(token).then(result => {   
      let kind = result[0].kind;
      if(kind === key){
         addComment(n, request.body);
         var success = { event : 'done' };
         return response.json(success);
      }
      else{
         var success1 = { event : 'not done' };
         return response.json(success1)
      }
   }).catch(error => console.log(error));
});

我尝试使用HttpClient从Windows Form应用程序中使用此服务

private async Task<string> PostAsyncToken(string token, string key, int num, 
    List<Comment> comments){
    HttpClient httpClient = new HttpClient();
    httpClient.BaseAddress = new Uri("http://myhost.com/");
    string uri = string.Format("v2/addComment?number={0}", num);

    httpClient.DefaultRequestHeaders.Accept.Clear();    
    httpClient.DefaultRequestHeaders.Accept.Add(new 
    MediaTypeWithQualityHeaderValue("application/json"));                
    httpClient.DefaultRequestHeaders.Add("token", token);
    httpClient.DefaultRequestHeaders.Add("key", key);                              

    var json = JsonConvert.SerializeObject(comments);
    var content = new StringContent(json, Encoding.UTF8, "application/json");

    var response = await httpClient.PostAsync(uri,content);
    string contentResponse = await response.Content.ReadAsStringAsync();
    return await Task.Run(() => contentResponse);   
}

测试响应总是返回404 Not Found,我猜这是因为在邮递员上工作时,请求在应为“ http”时会自动添加“ https”

0 个答案:

没有答案