我正在使用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
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
}
}
我添加了评论,让您了解我想要做的事情。实际上,我希望从响应中获得image.timeStamp
值。
谢谢!
答案 0 :(得分:4)
您可以将回复存储在词典中,然后您可以var x= dic["timeStamp"];
访问任何成员,也可以通过将dic
转换为dynamic object
来扩展实施。
编辑:
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]);
}
答案 1 :(得分:0)
这是我做的工作。 (在doe_deo回答的帮助下)
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
}
}