繁体   English   中英

C# HTTPClient 未发送请求

[英]C# HTTPClient Not Sending Request

我有一个 WPF 应用程序,我需要从远程 API 检索配置数据。 API 已经过测试,并且工作正常,返回了我认为正确的格式。 我已经为响应等创建了一个数据类型,但是当我到达通过 HttpClient 获取响应的行时,它不会向 API 发送任何请求。

数据类型:-

public class Mylist
    {
        public string ItemName { get; set; }
        public string ItemText { get; set; }
        public string ConfigName { get; set; }
        public string ConfigValue { get; set; }
        public string ConfigType { get; set; }
    }

代码:-

string licKey = ConfigManager.GetSetting("Lic");
            string Uri = ConfigManager.GetSetting("API");
            string UserAPI = ConfigManager.GetSetting("Config");

            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(Uri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var response = await client.GetAsync(UserAPI + "?Licence=" + licKey);
            var data = await response.Content.ReadAsAsync<IEnumerable<Mylist>>();
            int count = 0;
            List<Mylist> nudges = new List<Mylist>((IEnumerable<Nudgelist>)data);
// Do something with the data

该代码为请求构建正确的 URL ( https://example.com/api/Uri?Licence=licencevalue ),如果手动输入到浏览器中,它会给出如下响应:-

<Mylist>
<ConfigName>cName</ConfigName>
<ConfigType>cType</ConfigType>
<ConfigValue>cValue</ConfigValue>
<ItemName>iName</NudgeName>
<ItemText>iText</NudgeText>
</Mylist>
<Mylist>
...
</Mylist>

当我运行上面的代码并单步执行时,我们会看到“var response = await client.GetAsync(UserAPI + "?Licence=" + licKey);" 这一行它只是跳过代码的 rest 并进入下一个调用,没有引发错误或发现任何故障。

I have run logs on the API and we are not seeing the request coming in, if we use an identical model of code for calling another API controller to call Uri2 ( https://example.com/api/Uri2?Licence=licencevalue )它工作正常。

尝试这个


if (response.IsSuccessStatusCode)
{
var stringData = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<IEnumerable<MyList>>(stringData);
}
else { var statusCode= response.StatusCode);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM