int pageSize = 500;
int total = pageSize;
DataTable _dt = new DataTable();
//循环调用 webservice 直至取完所有数据.
for (int i = 1; i <= Math.Ceiling((double)(total * 1.0 / pageSize)); i++)
{
//要调用webservice 的地址 string _url = "http://url";
//生成一个实例
var request = (HttpWebRequest)WebRequest.Create(_url); //Header增加参数以键值对方式增加: key:value request.Headers.Add("Authorization", Authorization); //内容类型 request.ContentType = "application/json"; //提交方式 request.Method = "POST"; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { //设置发送的body json 参数 string json = new JavaScriptSerializer().Serialize(new { filter = new { startDate = "", endDate = "", Code = "" }, pageIndex = i, pageSize = pageSize }); //GetRequestStream 方法返回用于发送 HttpWebRequest 数据的流。返回 Stream 对象后,可以通过使用 Stream.Write 方法发送带有 HttpWebRequest 的数据。 streamWriter.Write(json); } //创建接收实例 var response = (HttpWebResponse)request.GetResponse(); //接收响应 using (var streamReader = new StreamReader(response.GetResponseStream())) { //响应字符串 string retString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(retString); //处理返回值中的total 属性 赋值给 原total 这里才是准确的 total total = Convert.ToInt32(jobject["data"]["total"].ToString()); //调用 PrivateMethods.JsonToDataTable 方法直接把 返回 json 串中 数组转换成了 datatable. DataTable temp = PrivateMethods.JsonToDataTable(retString); //把本次获取到的数据,插入到 _dt 表中. foreach (DataRow dr in temp.Rows) { _dt.ImportRow(dr); } }
}