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;
});
}
C# 發送http方法,利用鍵值對 KeyValuePair,發送分頁命令,日歷的開始和結束時間命令(POST)