- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 azure 函数,可以通过基本身份验证调用 Rest API。服务器的响应没问题,但我有一个问题,只接收从响应返回的部分数据 - 某些字段返回为空。
我正在使用 httpclient 进行其余 API 调用。这是我的代码:
using (var client = new HttpClient())
{
var webUrl = "https://api.livechatinc.com/chats/XXXXXX";
client.BaseAddress = new Uri(webUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var user = "XXXXXXXXXX";
var password = "XXXXXXXXX";
var base64String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{user}:{password}"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64String);
HttpResponseMessage response = await client.GetAsync(webUrl);
response.EnsureSuccessStatusCode();
//Only getting partial information
string jsonString = await response.Content.ReadAsStringAsync();
//Deserialise JSON
Chat responseData = JsonConvert.DeserializeObject<Chat>(jsonString);
}
当我运行我的 azure 函数调试器时,我可以证明我得到了我期望的 URL 数据的空字段。
请注意,反序列化后,responseData 字段显示 ChatStartUrl 的空值。
这是因为该字段不是从响应内容中返回的。
我已确定这不是我的数据合约和 JSON.net 反序列化。我的数据契约(Contract)是:
public class Chat
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("tickets")]
public Ticket[] Tickets { get; set; }
[JsonProperty("visitor_name")]
public string VisitorName { get; set; }
[JsonProperty("visitor_id")]
public string VisitorId { get; set; }
[JsonProperty("visitor_ip")]
public string VisitorIp { get; set; }
[JsonProperty("visitor")]
public Visitor Visitor { get; set; }
[JsonProperty("agents")]
public Agent[] Agents { get; set; }
[JsonProperty("supervisors")]
public object[] Supervisors { get; set; }
[JsonProperty("rate")]
public string Rate { get; set; }
[JsonProperty("duration")]
public long Duration { get; set; }
[JsonProperty("chat_start_url")]
public Uri ChatStartUrl { get; set; }
[JsonProperty("referrer")]
public Uri Referrer { get; set; }
[JsonProperty("group")]
public long[] Group { get; set; }
[JsonProperty("started")]
public string Started { get; set; }
[JsonProperty("custom_variables")]
public object[] CustomVariables { get; set; }
[JsonProperty("pending")]
public bool Pending { get; set; }
[JsonProperty("tags")]
public object[] Tags { get; set; }
[JsonProperty("timezone")]
public string Timezone { get; set; }
[JsonProperty("greeting")]
public Greeting Greeting { get; set; }
[JsonProperty("messages")]
public Event[] Messages { get; set; }
/* [JsonProperty("prechat_survey")]
public PrechatSurvey[] PrechatSurvey { get; set; }*/
[JsonProperty("events")]
public Event[] Events { get; set; }
[JsonProperty("engagement")]
public string Engagement { get; set; }
[JsonProperty("started_timestamp")]
public long StartedTimestamp { get; set; }
[JsonProperty("ended_timestamp")]
public long EndedTimestamp { get; set; }
[JsonProperty("ended")]
public string Ended { get; set; }
}
public class Agent
{
[JsonProperty("display_name")]
public string DisplayName { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("ip")]
public string Ip { get; set; }
}
public class Event
{
[JsonProperty("author_name", NullValueHandling = NullValueHandling.Ignore)]
public string AuthorName { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
[JsonProperty("message_json", NullValueHandling = NullValueHandling.Ignore)]
public string MessageJson { get; set; }
[JsonProperty("date")]
public string Date { get; set; }
[JsonProperty("timestamp")]
public long Timestamp { get; set; }
[JsonProperty("agent_id", NullValueHandling = NullValueHandling.Ignore)]
public string AgentId { get; set; }
[JsonProperty("user_type")]
public string UserType { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("welcome_message", NullValueHandling = NullValueHandling.Ignore)]
public bool? WelcomeMessage { get; set; }
[JsonProperty("event_type", NullValueHandling = NullValueHandling.Ignore)]
public string EventType { get; set; }
}
public class Greeting
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
public class Ticket
{
[JsonProperty("id")]
public string Id { get; set; }
}
public class Visitor
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("ip")]
public string Ip { get; set; }
[JsonProperty("city")]
public string City { get; set; }
[JsonProperty("region")]
public string Region { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("country_code")]
public string CountryCode { get; set; }
[JsonProperty("timezone")]
public string Timezone { get; set; }
[JsonProperty("user_agent")]
public string UserAgent { get; set; }
}
有趣的是,其余 api 在 Postman 中运行良好,我可以取回所有数据。
这是我从 postman 回复中得到的内容
{
"type": "chat",
"id": "PPTE7XI3AQ",
"tickets": [
{
"id": "DJWKT"
}
],
"visitor_name": "Rupinder Kaur",
"visitor_id": "S1552009602.e395581450",
"visitor_ip": "49.183.59.243",
"visitor": {
"id": "S1552009602.e395581450",
"name": "Rupinder Kaur",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbc9cecbd2d5dfdec9fbd8c9dedacfd2cdda95d8d4d695dace" rel="noreferrer noopener nofollow">[email protected]</a>",
"ip": "49.183.59.243",
"city": "Abbotsford",
"region": "Victoria",
"country": "Australia",
"country_code": "AU",
"timezone": "Australia/Victoria",
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
},
"agents": [
{
"display_name": "David",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b4f4a5d424f05584e454f4e596b5c4242584e05484446" rel="noreferrer noopener nofollow">[email protected]</a>",
"ip": "203.30.93.14"
}
],
"supervisors": [],
"rate": "not_rated",
"duration": 357,
"chat_start_url": "https://wiise.com/au/explore/detailed-accounting/?utm_source=googlewiise&utm_medium=paidsearch&utm_campaign=Wiise&gclid=EAIaIQobChMI3ZrLuqG14QIVBR2PCh0VIAb7EAAYASAAEgKkWPD_BwE",
"referrer": "https://www.google.com/",
"group": [
0
],
"started": "Thu, 04/04/19 12:25:43 pm",
"custom_variables": [],
"pending": false,
"tags": [],
"timezone": "Australia/Sydney",
"greeting": {
"id": 7991,
"name": "Invite after 360 seconds"
},
"messages": [
{
"author_name": "David",
"text": "Hi there :) \n\nThank you for checking out our website, how can I help?",
"message_json": "",
"date": "Thu, 04/04/19 12:25:43 pm",
"timestamp": 1554341143,
"agent_id": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4024213629246e33252e2425320037292933256e232f2d" rel="noreferrer noopener nofollow">[email protected]</a>",
"user_type": "agent",
"type": "message",
"welcome_message": true
},
{
"author_name": "Rupinder Kaur",
"text": "hi there",
"message_json": "",
"date": "Thu, 04/04/19 12:25:54 pm",
"timestamp": 1554341154,
"user_type": "visitor",
"type": "message"
},
{
"author_name": "Rupinder Kaur",
"text": "I would like to trial it",
"message_json": "",
"date": "Thu, 04/04/19 12:26:01 pm",
"timestamp": 1554341161,
"user_type": "visitor",
"type": "message"
},
{
"author_name": "Rupinder Kaur",
"text": "I am mainly looking for cashflow reports",
"message_json": "",
"date": "Thu, 04/04/19 12:26:25 pm",
"timestamp": 1554341185,
"user_type": "visitor",
"type": "message"
}
],
"prechat_survey": [
{
"key": "Name:",
"value": "xxxxxx",
"id": "2001"
},
{
"key": "E-mail:",
"value": "xxxxxx",
"id": "2002"
}
],
"events": [
{
"author_name": "David",
"text": "Hi there :) \n\nThank you for checking out our website, how can I help?",
"message_json": "",
"date": "Thu, 04/04/19 12:25:43 pm",
"timestamp": 1554341143,
"agent_id": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="771316011e135904121913120537001e1e04125914181a" rel="noreferrer noopener nofollow">[email protected]</a>",
"user_type": "agent",
"type": "message",
"welcome_message": true
},
{
"author_name": "XXXXXX",
"text": "XXXXXXXXXX",
"message_json": "",
"date": "Thu, 04/04/19 12:25:54 pm",
"timestamp": 1554341154,
"user_type": "visitor",
"type": "message"
},
{
"author_name": "XXXXXX",
"text": "XXXXXXXXXX",
"message_json": "",
"date": "Thu, 04/04/19 12:26:01 pm",
"timestamp": 1554341161,
"user_type": "visitor",
"type": "message"
},
{
"author_name": "XXXXXX",
"text": "XXXXXXXXXX",
"message_json": "",
"date": "Thu, 04/04/19 12:26:25 pm",
"timestamp": 1554341185,
"user_type": "visitor",
"type": "message"
},
{
"text": "Rupinder Kaur left the chat.",
"date": "Thu, 04/04/19 12:31:40 pm",
"timestamp": 1554341500,
"type": "event",
"event_type": "closed",
"user_type": "visitor"
}
],
"engagement": "auto_invite",
"started_timestamp": 1554341143,
"ended_timestamp": 1554341500,
"ended": "Thu, 04/04/19 12:31:40 pm"}
我尝试反序列化从 Postman 返回的内容,我得到了所有没有空字段的数据
//This works
var testJSON = System.IO.File.ReadAllText(@"C:\temp\ChatSchema.json");
Chat responseData = JsonConvert.DeserializeObject<Chat>(testJSON);
因此我怀疑 Content.ReadAsStringAsync 函数未按预期工作。
我尝试了以下方法,但仍然遇到同样的问题:
var buffer = await response.Content.ReadAsByteArrayAsync();
var responseString = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
string content = responseString;
Chat des = JsonConvert.DeserializeObject<Chat>(content);
最佳答案
读取原始文本,然后将其反序列化到模型类。我还必须更改序列化例程以另存为“UTF-8”。
您可以引用以下代码:
var stream = response.Content.ReadAsStreamAsync().Result;
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
string content = text;
string des = JsonConvert.DeserializeObject<Chat>(content);
关于azure - response.Content.ReadAsStringAsync 不读取所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55584000/
什么是 SASS 方法要求干燥这样的东西: .content p, .content ul, .content li, .content a 最佳答案 这 4 个元素都有共同的样式吗? .conten
我正在评估 Contentful.com 作为 Angular SPA 的内容引擎。 我面临的问题是按内容类型检索条目(例如,获取“博客”类型的所有条目)。如 documentation exampl
在我编辑的主 wiki 上有一个名为 Item: 的自定义命名空间,提示是该命名空间内的每个页面都显示为 Item:This_item - - Item:That_item -- Item:Foo_i
我正在尝试编写一个Python脚本,可以将图片和pdf上传到WordPress。我希望图像上传到文件夹‘/wp-Content/Uploads/’,将pdf文件上传到文件夹‘/wp-Content/U
是否可以监控进行了多少次 Contentful API 调用,并理想地在即将超过配额时收到通知? 谢谢 最佳答案 当然,您可以在右侧用户配置文件的下拉菜单 > Organizations and Bi
我在尝试在 IE8 中下载带有分号的文件名时遇到问题。 Response.AddHeader("Content-Disposition", "attachment; filename=\"" + at
我在 Contentful Delivery API 中运行查询以返回基于它的 slug 的特定页面项目。这个查询还设置了语言环境,以便它只返回我需要呈现的语言的数据。 但是,我还需要设置页面的 hr
我有兴趣使用 Gatsby建一个Netlify使用来自 Contentful 的内容的静态网站 Netlify 有这个不错的 Gatsby 入门指南: https://www.netlify.com/
目标是提交一个 git 分支。分支的“git status”的输出是: On branch zeromq_new Your branch is up to date with 'origin/zero
我目前正在学习在 ASP.NET 3.5 和 C# 中使用 MasterPages 和 ContentPlaceHolders - 现在,我正在拼命尝试通过我的编程代码编辑 asp:Content-C
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我使用 bootstrap 3 作为我的网格框架和 CSS 来创建一个具有一个倾斜/倾斜边缘的半透明区域,但由于分层不透明度,我的元素遇到了问题。 期望是中心是倾斜的,但右侧仍然是正方形。 有没有更好
IllegalArgumentException: 未知 URL 内容:// ^ 对上述内容做了噩梦。我检查了我的变量和路径,但看不出问题是什么?非常感谢任何指点! 这是我的痕迹。 java.lan
我有两个元素:一个是 元素,另一个是 元素。 populated-drop-down extends drop-down ,但是,正如您可能已经猜到的那样,它会尝试使用一些选项预先填充它。假设我可以简
我想我也有同样的问题。 Using multiple yields to insert content 我尝试了这个解决方案。我试过 在我的 application.html.erb 中有 conte
此链接 ( https://css-tricks.com/snippets/css/a-guide-to-flexbox/ ) 表示 justify-content 和 align-content 的
我现在正在探索绑定(bind),并且有一个 NSPopUpButton - 它为我提供了一些值选择下的绑定(bind)选项 - Content , Content Objects , Content
正在尝试在内容页面中加载内容 View 。当我运行代码时,它不会出现在我的内容 View 中。我正在从我的内容页面分配两个可绑定(bind)参数。 内容页面: 内容 View :
我想从我的 :before 标签中获取 content。我知道有些人会说它不是真正的(伪)元素,但在 JS 中有一种方法,但有人可以帮助我在 JQ 中做到这一点,因为我有多个标签并且我想用 $.eac
我创建了一个.sh脚本,并将结果记录在一个文件中,执行后我会尝试将文件内容作为mail正文发送。 这是我运行的命令: sh update.sh >> update.$(date +"%Y-%m-%d:
我是一名优秀的程序员,十分优秀!