C# ReadAsAsync始终返回空WPF
我正在尝试获取bin为“0001”的数据C# ReadAsAsync始终返回空WPF,c#,wpf,mvvm,get,caliburn.micro,C#,Wpf,Mvvm,Get,Caliburn.micro,我正在尝试获取bin为“0001”的数据 string bin=“0001” HttpClient=新的HttpClient(); 字符串uri=$”https://localhost:44316/api/Bplo?bin={bin}”; HttpResponseMessage response=wait client.GetAsync(uri); if(响应。IsSuccessStatusCode) { var result=await response.Content.ReadAsAsync
string bin=“0001”
HttpClient=新的HttpClient();
字符串uri=$”https://localhost:44316/api/Bplo?bin={bin}”;
HttpResponseMessage response=wait client.GetAsync(uri);
if(响应。IsSuccessStatusCode)
{
var result=await response.Content.ReadAsAsync();
返回结果;
}
其他的
{
抛出新异常(response.ReasonPhrase);
}
- 状态代码为200,但结果返回为空:
- 我检查了问题是否是端点,但效果很好:
- 我试着检查我是否拼错了模型:
尝试将其添加到列表中,但仍然返回null。只需添加.Result(),在ReasAsync的末尾查看您的屏幕截图,您的端点似乎使用了合理的JSON进行响应,您的模型似乎正常,并且您从调用中获得了HTTP 200 我深入研究并分解了它的组成部分,以便您可以逐步了解哪个部分不适合您:
public static async Task<T> MyReadAsAsync<T>(string url)
{
var response = await new HttpClient().GetAsync(url);
response.EnsureSuccessStatusCode(); // Throw up if unsuccessful
/*** ReadAsAsync<> starts here ***/
// Check the header for content type
var contentType = response.Content.Headers.ContentType;
// Expected "application/json"
Debug.WriteLine($"ContentType: {contentType}");
// Get available formatters in the system
var formatters = new MediaTypeFormatterCollection();
// Expected: more than 0
Debug.WriteLine($"Formatters: {formatters.Count}");
// Find the appropriate formatter for the content
var formatter = formatters.FindReader(typeof(T), contentType);
// Expected: JsonMediaTypeFormatter
Debug.WriteLine($"Formatter: {formatter}");
// Check the formatter
var canRead = formatter.CanReadType(typeof(T));
// Expected: true
Debug.WriteLine($"CanReadType: {canRead}");
// Check the stream
var stream = await response.Content.ReadAsStreamAsync();
// Expected: length of your JSON
Debug.WriteLine($"StreamLength: {stream.Length}");
// Expected: your JSON here
Debug.WriteLine(System.Text.Encoding.UTF8.GetString(
(stream as System.IO.MemoryStream)?.ToArray()));
// Check the formatter reading and converting
// from the stream into an object
var resultObj = await formatter.ReadFromStreamAsync(
typeof(T), stream, response.Content, null);
// Expected: an object of your type
Debug.WriteLine($"Obj: {resultObj}");
// Cast to the proper type
var result = (T)resultObj;
// Expected: an object of your type
Debug.WriteLine($"Result: {result}");
return result;
}
公共静态异步任务MyReadAsAsync(字符串url)
{
var response=wait new HttpClient().GetAsync(url);
response.EnsureSuccessStatusCode();//如果失败则抛出
/***ReadAsAsync从这里开始***/
//检查标题的内容类型
var contentType=response.Content.Headers.contentType;
//应为“application/json”
WriteLine($“ContentType:{ContentType}”);
//在系统中获取可用的格式化程序
var formatters=新的MediaTypeFormatterCollection();
//预期:超过0
WriteLine($“格式化程序:{Formatters.Count}”);
//为内容查找适当的格式化程序
var formatter=formatters.FindReader(typeof(T),contentType);
//应为:JsonMediaTypeFormatter
WriteLine($“格式化程序:{Formatter}”);
//检查格式化程序
var canRead=formatter.CanReadType(typeof(T));
//预期:正确
WriteLine($“CanReadType:{canRead}”);
//检查小溪
var stream=await response.Content.ReadAsStreamAsync();
//预期:JSON的长度
Debug.WriteLine($“StreamLength:{stream.Length}”);
//预期:您的JSON在这里
Debug.WriteLine(System.Text.Encoding.UTF8.GetString(
(流作为System.IO.MemoryStream)?.ToArray());
//检查格式化程序的读取和转换
//从流到对象
var resultObj=await formatter.ReadFromStreamAsync(
typeof(T),stream,response.Content,null);
//应为:您类型的对象
Debug.WriteLine($“Obj:{resultObj}”);
//铸造成合适的类型
var result=(T)resultObj;
//应为:您类型的对象
WriteLine($“Result:{Result}”);
返回结果;
}
您可以通过传入url来调用该方法(另请参阅,了解针对公共端点的工作测试)
var result=等待MyReadAsAsync(“https://YOUR_ENDPOINT");
仍然给出错误。在现代.Net中,调用结果
或等待
是很难接受的。总有一种更好的方法,通常是等待
@我需要创建一个接口并按1分配属性吗?你需要证明你的有效负载是你所认为的。使用Fiddler,或者获取字符串,这是一个HTTP响应。您确定响应包含BploModel对象而不是字节数组或s字符串吗?看起来内容无法转换为遗传参数类型。也许您应该尝试一种原始的HttpContent方法,比如ReadAsStringAsync。您需要手动反序列化任何字节数据。像类这样的数据类型是一种语言细节。HTTP规范不支持特定于语言的日期类型。正如@thegeneral所说,使用Fiddler。但是,由于您所做的只是一个GET,所以只需在浏览器中进行测试,看看服务器是什么returning@BionicCode你说得对,我的有效载荷不匹配。代码在我将字节数据转换为对象后工作。非常感谢你!!让我避免再进行4小时的调试,我如何标记您的答案?
public static async Task<T> MyReadAsAsync<T>(string url)
{
var response = await new HttpClient().GetAsync(url);
response.EnsureSuccessStatusCode(); // Throw up if unsuccessful
/*** ReadAsAsync<> starts here ***/
// Check the header for content type
var contentType = response.Content.Headers.ContentType;
// Expected "application/json"
Debug.WriteLine($"ContentType: {contentType}");
// Get available formatters in the system
var formatters = new MediaTypeFormatterCollection();
// Expected: more than 0
Debug.WriteLine($"Formatters: {formatters.Count}");
// Find the appropriate formatter for the content
var formatter = formatters.FindReader(typeof(T), contentType);
// Expected: JsonMediaTypeFormatter
Debug.WriteLine($"Formatter: {formatter}");
// Check the formatter
var canRead = formatter.CanReadType(typeof(T));
// Expected: true
Debug.WriteLine($"CanReadType: {canRead}");
// Check the stream
var stream = await response.Content.ReadAsStreamAsync();
// Expected: length of your JSON
Debug.WriteLine($"StreamLength: {stream.Length}");
// Expected: your JSON here
Debug.WriteLine(System.Text.Encoding.UTF8.GetString(
(stream as System.IO.MemoryStream)?.ToArray()));
// Check the formatter reading and converting
// from the stream into an object
var resultObj = await formatter.ReadFromStreamAsync(
typeof(T), stream, response.Content, null);
// Expected: an object of your type
Debug.WriteLine($"Obj: {resultObj}");
// Cast to the proper type
var result = (T)resultObj;
// Expected: an object of your type
Debug.WriteLine($"Result: {result}");
return result;
}
var result = await MyReadAsAsync<BploModel>("https://YOUR_ENDPOINT");