C# 将响应转换为可访问对象属性
我正在使用HttpClient从链接获取数据 以下是我的回应:C# 将响应转换为可访问对象属性,c#,asp.net,asp.net-web-api,httpclient,C#,Asp.net,Asp.net Web Api,Httpclient,我正在使用HttpClient从链接获取数据 以下是我的回应: #S7Z OK #Mon Dec 26 02:26:58 EST 2016 image.anchor=168,186 image.embeddedIccProfile=0 image.embeddedPhotoshopPaths=0 image.embeddedXmpData=0 image.expiration=-1.0 image.height=373 image.iccProfile=sRGB IEC61966-2.1 ima
#S7Z OK
#Mon Dec 26 02:26:58 EST 2016
image.anchor=168,186
image.embeddedIccProfile=0
image.embeddedPhotoshopPaths=0
image.embeddedXmpData=0
image.expiration=-1.0
image.height=373
image.iccProfile=sRGB IEC61966-2.1
image.mask=1
image.photoshopPathnames=
image.pixTyp=RGB
image.printRes=72
image.resolution=34
image.thumbRes=17
image.thumbType=2
image.timeStamp=1481737849826
image.width=336
我想将此响应转换为可访问对象
这是我的httpclient
工作:
using (var client = getHttpClient())
{
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
//var imageData = await response.Content.ReadAsAsync<imageData>();
//imageData.timeStamp
}
else
{
//TODO: Need to handle error scenario
}
}
使用(var client=getHttpClient())
{
HttpResponseMessage response=wait client.GetAsync(路径);
if(响应。IsSuccessStatusCode)
{
//var imageData=await response.Content.ReadAsAsync();
//imageData.timeStamp
}
其他的
{
//TODO:需要处理错误场景
}
}
我添加了一些评论,让你知道我想做什么。实际上,我想从响应中获取image.timeStamp
值
谢谢 您可以通过将响应存储在Dictionary中来实现这一点,然后您可以访问任何成员作为
var x=dic[“timeStamp”]
,您还可以通过将dic
转换为动态对象
来扩展实现
编辑:
您可以通过将响应存储在Dictionary中来实现这一点,然后您可以作为
var x=dic[“timeStamp”]访问任何成员
,您还可以通过将dic
转换为动态对象
来扩展实现
编辑:
以下是我所做的工作,让它发挥作用。(在doe_deo回答的帮助下)
使用(var client=getHttpClient())
{
HttpResponseMessage response=wait client.GetAsync(路径);
if(响应。IsSuccessStatusCode)
{
var data=await response.Content.ReadAsStringAsync();
字典=新字典();
var rx=新的正则表达式(@“(.*?)\s*=\s*([^\s]+)”;
foreach(接收中匹配m。匹配(数据))
{
添加(m.Groups[1].ToString(),m.Groups[2].ToString());
}
}
其他的
{
//TODO:需要处理错误场景
}
}
以下是我所做的工作。(在doe_deo回答的帮助下)
使用(var client=getHttpClient())
{
HttpResponseMessage response=wait client.GetAsync(路径);
if(响应。IsSuccessStatusCode)
{
var data=await response.Content.ReadAsStringAsync();
字典=新字典();
var rx=新的正则表达式(@“(.*?)\s*=\s*([^\s]+)”;
foreach(接收中匹配m。匹配(数据))
{
添加(m.Groups[1].ToString(),m.Groups[2].ToString());
}
}
其他的
{
//TODO:需要处理错误场景
}
}
谢谢您的回答。您能告诉我如何将此响应数据转换为字典吗?这似乎是一个不错的选择。谢谢但字典里有一些错误的值。我已经用正则表达式修复了它。谢谢你的回答。您能告诉我如何将此响应数据转换为字典吗?这似乎是一个不错的选择。谢谢但字典里有一些错误的值。我用正则表达式来修正它。
Stream receiveStream = response.GetResponseStream ();
StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
var text = readStream.ReadToEnd();
// Split the content into chunks
foreach(var ch in chunks)
{
string[] kv = ch.Split('=');
dic.Add(kv[0], kv[1]);
}
using (var client = getHttpClient())
{
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
Dictionary<string, string> dictionary = new Dictionary<string, string>();
var rx = new Regex(@"(.*?)\s*=\s*([^\s]+)");
foreach (Match m in rx.Matches(data))
{
dictionary.Add(m.Groups[1].ToString(), m.Groups[2].ToString());
}
}
else
{
//TODO: Need to handle error scenario
}
}