C# 发送http方法,利用键值对 KeyValuePair,发送分页命令,日历的开始和结束时间命令(POST)
一个HTTP的类里面:
public static string HttpClientPost(string url, string taskIdx, string taskidxvalue, string source_ID, string source_ID_value, string StartTime, string StartTimeValue, string endTime, string endTimeValue, string startNum, string startNumValue, string pageSize, string pageSizeValue)
{
string resultContent = "";
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>(taskIdx, taskidxvalue),
new KeyValuePair<string, string>(source_ID, source_ID_value),
new KeyValuePair<string, string>(StartTime, startNumValue),
new KeyValuePair<string, string>(endTime, endTimeValue),
new KeyValuePair<string, string>(startNum, startNumValue),
new KeyValuePair<string, string>(pageSize, pageSizeValue)
});
var result = client.PostAsync(url, content).Result;
resultContent = result.Content.ReadAsStringAsync().Result;
}
}
catch (Exception ex)
{
}
return resultContent;
}
.cs调用:
(包括分页,日历的开始和结束时间)
public CompareNew_Json GetHttpJsonToOb(string url, string taskIdx = "", string taskidxvalue = "", string source_ID = "", string source_ID_value = "", string StartTime = "", string StartTimeValue = "", string endTime = "", string endTimeValue = "", string startNum = "", string startNumValue = "", string pageSize = "", string pageSizeValue = "")
{
CompareNew_Json rt = null;
string resultContent = HttpRequest.HttpClientPost(url, taskIdx, taskidxvalue, source_ID, source_ID_value, StartTime, StartTimeValue, endTime, endTimeValue, startNum, startNumValue, pageSize, pageSizeValue);
if (resultContent != "")
{
rt = JsonConvert.DeserializeObject<CompareNew_Json>(resultContent);
MessageBox.Show(rt.alarmCount.ToString());
if (rt.alarmCount % 32 == 0)
{
pageSum = rt.alarmCount / 32;
tb_sumnum.Content = pageSum;
}
else
{
pageSum = (rt.alarmCount / 32) + 1;
tb_sumnum.Content = pageSum;
}
resultContent = "";
}
return rt;
}
调用上面的方法:
public void SaySomething(string starttime = "", string endtime = "")
{
string _starttime = "";
string _endtime = "";
if (starttime != "" && endtime != "")
{
_starttime = "StartTime";
_endtime = "endTime";
}
Dispatcher.Invoke(() => {
CompareNew_Json json = GetHttpJsonToOb("http://192.168.20.66:8080/SSMFace05/history/alarm/getDynamic", StartTime: _starttime, StartTimeValue: starttime, endTime: _endtime, endTimeValue: endtime, startNum: "startNum", startNumValue: pagenum.ToString(), pageSize: "pageSize", pageSizeValue: "32");
DataAnalysisdataGrid = new List<DataGridBindingData>();
tb_num.Content = pagenum;
if (json.alarms != null)
{
for (int i = 0; i < json.alarms.Count; i++)
{
DataGridBindingData dgd = new DataGridBindingData();
dgd.dataID = i.ToString();
dgd.Time = json.alarms[i].alarmTime;
dgd.VideoSource = json.alarms[i].source.sourceName;
dgd.goalpeople = json.alarms[i].target.targetName;
dgd.CallTimes = ((json.alarms[i].alarmSimilar) * 100).ToString() + "%";
DataAnalysisdataGrid.Add(dgd);
}
}
dataGrid.ItemsSource = DataAnalysisdataGrid;
});
}