一、代码
①类名
public class TouTiao { public class model { public string site_id { get; set; } public string ad_id { get; set; } public List<Data> data { get; set; } } public class Data { public string label { get; set; } public string lable { get; set; } public string value { get; set; } } }
②控制器
public class DataController : BaseController { [HttpPost] public int GetTT() { using (CR_ReservationRepository db = new CR_ReservationRepository(new Models.DBContainer())) { //正规流程 //1.UrlDecode: //2.unicode 转中文 //var e = "{ "site_id": "1598708326294535", "ad_id": "", "data": [{"lable": "u623fu5b50u9762u79ef", "id": 1599334023368723, "value": "321", "label": "u623fu5b50u9762u79ef"}]}"; var e = System.Web.HttpContext.Current.Request.Form["data"]; var h = Newtonsoft.Json.JsonConvert.DeserializeObject<TouTiao.model>(e); var entity = new Models.CR_Reservation(); try { entity.Flag = "今日头条"; entity.Status = 0; entity.CreateTime = Utils.ObjectToDateTime( GetValue(h.data, "时间") ); entity.Tel = GetValue(h.data, "手机"); entity.Name = GetValue(h.data, "称呼"); entity.Area = GetValue(h.data, "面积"); entity.OpenSoure = e; } catch { entity.Flag = "错误"; entity.OpenSoure = e??"null"; entity.Status = 0; entity.CreateTime = DateTime.Now; } db.Create(entity); return db.SaveChanges(); } } private string GetValue(List<TouTiao.Data> m, string str) { var s = ""; try { s = m.FirstOrDefault(x => x.label.Contains(str)).value; } catch { } return s; } }
二、过程分析
1.看官网文档。curl自己本地代码测试成功。但是不见今日数据过来,怀疑今日头条是否转发。
2.分析是否转发。查看IIS站点日志,发现有数据转发过来,准备监控流量
3.监控访问请求。打开WiresharkPortable,监控iis访问IP。查看请求过程,服务器报500错误,原来表单是按键值对传值。Rquest.Form["data"]获取到数据,ok