LBS云端数据删除和上传
这里采用C#模拟表单提交,实现LBS云端删除和csv格式文件的上传。
删除:
1 /// <summary> 2 /// 从LBS云端删除数据 3 /// </summary> 4 /// <param name="ak">百度密钥</param> 5 /// <param name="geotable_id">LBS云geotableID</param> 6 /// <param name="columnName">字段名称</param> 7 /// <param name="columnValue">值(为空时则删除所有匹配的数据)</param> 8 /// <returns></returns> 9 public string DeleteByName(string ak, string geotable_id, string columnName, string columnValue) 10 { 11 string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); 12 //请求 13 WebRequest req = WebRequest.Create(@"http://api.map.baidu.com/geodata/v2/poi/delete"); 14 req.Method = "POST"; 15 req.ContentType = "multipart/form-data; boundary=" + boundary; 16 //组织表单数据 17 StringBuilder sb = new StringBuilder(); 18 sb.Append("--" + boundary); 19 sb.Append("\r\n"); 20 21 sb.Append("Content-Disposition: form-data; name=\"ak\""); 22 sb.Append("\r\n\r\n"); 23 sb.Append(ak); 24 sb.Append("\r\n"); 25 sb.Append("--" + boundary); 26 sb.Append("\r\n"); 27 28 sb.Append("Content-Disposition: form-data; name=\"geotable_id\""); 29 sb.Append("\r\n\r\n"); 30 sb.Append(geotable_id); 31 sb.Append("\r\n"); 32 sb.Append("--" + boundary); 33 sb.Append("\r\n"); 34 35 sb.Append("Content-Disposition: form-data; name=\"" + columnName + "\""); 36 sb.Append("\r\n\r\n"); 37 sb.Append(columnValue); 38 sb.Append("\r\n"); 39 sb.Append("--" + boundary); 40 sb.Append("\r\n"); 41 42 43 string head = sb.ToString(); 44 byte[] form_data = Encoding.UTF8.GetBytes(head); 45 //结尾 46 byte[] foot_data = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); 47 48 //post总长度 49 long length = form_data.Length + foot_data.Length; 50 req.ContentLength = length; 51 Stream requestStream = req.GetRequestStream(); 52 53 //发送表单参数 54 requestStream.Write(form_data, 0, form_data.Length); 55 56 //结尾 57 requestStream.Write(foot_data, 0, foot_data.Length); 58 requestStream.Close(); 59 60 61 //响应 62 WebResponse pos = req.GetResponse(); 63 StreamReader sr = new StreamReader(pos.GetResponseStream(), Encoding.UTF8); 64 string html = sr.ReadToEnd().Trim(); 65 sr.Close(); 66 if (pos != null) 67 { 68 pos.Close(); 69 pos = null; 70 } 71 if (req != null) 72 { 73 req = null; 74 } 75 return html; 76 }
上传csv格式文件:
1 /// <summary> 2 /// 模拟表单提交,实现提交csv格式文件的上传 3 /// </summary> 4 /// <param name="ak"></param> 5 /// <param name="geotable_id"></param> 6 /// <param name="file"></param> 7 /// <returns></returns> 8 public string UploadCSVFile(string ak, string geotable_id, string filePath) 9 { 10 string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); 11 //请求 12 WebRequest req = WebRequest.Create(@"http://api.map.baidu.com/geodata/v2/poi/upload"); 13 req.Method = "POST"; 14 req.ContentType = "multipart/form-data; boundary=" + boundary; 15 //组织表单数据 16 StringBuilder sb = new StringBuilder(); 17 sb.Append("--" + boundary); 18 sb.Append("\r\n"); 19 sb.Append("Content-Disposition: form-data; name=\"ak\""); 20 sb.Append("\r\n\r\n"); 21 sb.Append(ak); 22 sb.Append("\r\n"); 23 sb.Append("--" + boundary); 24 sb.Append("\r\n"); 25 sb.Append("Content-Disposition: form-data; name=\"geotable_id\""); 26 sb.Append("\r\n\r\n"); 27 sb.Append(geotable_id); 28 sb.Append("\r\n"); 29 sb.Append("--" + boundary); 30 sb.Append("\r\n"); 31 sb.Append("Content-Disposition: form-data; name=\"poi_list\"; filename=\"e:\\databox.csv\""); 32 sb.Append("\r\n"); 33 sb.Append("Content-Type: application/octet-stream"); 34 sb.Append("\r\n\r\n"); 35 36 string head = sb.ToString(); 37 byte[] form_data = Encoding.UTF8.GetBytes(head); 38 //结尾 39 byte[] foot_data = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); 40 //文件 41 FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); 42 //post总长度 43 long length = form_data.Length + fileStream.Length + foot_data.Length; 44 req.ContentLength = length; 45 Stream requestStream = req.GetRequestStream(); 46 47 //发送表单参数 48 requestStream.Write(form_data, 0, form_data.Length); 49 //文件内容 50 byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))]; 51 int bytesRead = 0; 52 while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) 53 requestStream.Write(buffer, 0, bytesRead); 54 //结尾 55 requestStream.Write(foot_data, 0, foot_data.Length); 56 requestStream.Close(); 57 58 59 //响应 60 WebResponse pos = req.GetResponse(); 61 StreamReader sr = new StreamReader(pos.GetResponseStream(), Encoding.UTF8); 62 string html = sr.ReadToEnd().Trim(); 63 sr.Close(); 64 if (pos != null) 65 { 66 pos.Close(); 67 pos = null; 68 } 69 if (req != null) 70 { 71 req = null; 72 } 73 return html; 74 }
调用:
1 string ak = System.Configuration.ConfigurationManager.AppSettings["ak"].ToString(); 2 string geotableID = System.Configuration.ConfigurationManager.AppSettings["geotableID"].ToString(); 3 DeleteByName(ak, geotableID, "title", ""); //删除所有数据 4 UploadCSVFile(ak, geotableID, @"E:\databox.csv"); //上传数据