0

我的程序员伙伴们,我基本上有这个异步 Get() 方法可以成功读取 Json 数据,但是在定位到路由时 -> localhost:59185/api/encompass/data 我收到一条消息:

No HTTP resource was found that matches the request URL 'http://localhost:59185/api/encompass/data'.
</Message>

我非常希望它会返回我的 JSON,尤其是在调试代码时,它位于底部的“字符串 res”中

任何人都知道为什么它不返回 Json 甚至认为它坐在“res”中?

控制器:

    [HttpGet, Route("encompass/data")]
    public async Task<string> Get(string Accesstoken)
    {
         string res = "";
         using (var client = new HttpClient())
        {
            Accesstoken = Accesstoken.Substring(17, 28);
            client.BaseAddress = new Uri("https://api.elliemae.com/");
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Accesstoken);
            var response = client.GetAsync("encompass/v1/loans/ea7c29a6-ee08-4816-99d2-fbcc7d15731d").Result;
            using (HttpContent content = response.Content)
            {
                // ... Read the string.
                Task<string> result = content.ReadAsStringAsync();
                res = result.Result;
            }

            return res; //<- this is not returning the JSon thats sitting in here 
        }

    }
4

1 回答 1

0

fay,您的 Get 方法的标头中使用的不记名令牌必须是 Base64 编码的

请参阅 [how-do-i-encode-and-decode-a-base64-string][1]

我为令牌添加了 Bearer HEADER 值。

  [HttpGet, Route("values/get")]
        public async Task<string> Get(string resulted)
        {

            string res = "";
            using (var client = new HttpClient())
            {
                // HTTP POST

                client.BaseAddress = new Uri("https://api.elliemae.com/");          
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(resulted)));
                var response = client.GetAsync("/encompass/v1/loans/{ea7c29a6-ee08-4816-99d2-fbcc7d15731d}?Authorization=Bearer "+resulted+"&Content-Type=application/json").Result;

                using (HttpContent content = response.Content)
                {
                    // ... Read the string.
                    Task<string> result = content.ReadAsStringAsync();
                    res = result.Result;
                }
            }
            return res;
        }
于 2019-06-21T16:52:19.153 回答