C#往百度LBS云上传文件总是返回“百度-您的访问出错了”,求解
函数原型public string UploadCSVFile(string ak, string geotable_id, string filePath)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
//请求
WebRequest req = WebRequest.Create(@"http://api.map.baidu.com/geodata/v3/poi/upload");
//WebRequest req = WebRequest.Create(@"http://api.map.baidu.com/geodata/v2/poi/upload");
req.Method = "POST";
req.ContentType = "multipart/form-data; boundary=" + boundary;
req.Proxy = WebRequest.GetSystemWebProxy();
//组织表单数据
StringBuilder sb = new StringBuilder();
sb.Append("--" + boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"ak\"");
sb.Append("\r\n\r\n");
sb.Append(ak);
sb.Append("\r\n");
sb.Append("--" + boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"geotable_id\"");
sb.Append("\r\n\r\n");
sb.Append(geotable_id);
sb.Append("\r\n");
sb.Append("--" + boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"poi_list\"; filename=\"e:\\databox_49612.csv\"");
sb.Append("\r\n");
sb.Append("Content-Type: application/octet-stream");
sb.Append("\r\n\r\n");
string head = sb.ToString();
byte[] form_data = Encoding.UTF8.GetBytes(head);
//结尾
byte[] foot_data = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
//文件
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
//post总长度
long length = form_data.Length + fileStream.Length + foot_data.Length;
req.ContentLength = length;
Stream requestStream = req.GetRequestStream();
//发送表单参数
requestStream.Write(form_data, 0, form_data.Length);
//文件内容
byte[] buffer = new Byte;
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);
//结尾
requestStream.Write(foot_data, 0, foot_data.Length);
requestStream.Close();
string html = string.Empty;
WebResponse pos = null;
//响应
try
{
pos = req.GetResponse();
StreamReader sr = new StreamReader(pos.GetResponseStream(), Encoding.GetEncoding("gb2312"));
html = sr.ReadToEnd().Trim();
sr.Close();
}
catch(Exception ex)
{
Console.WriteLine("GetResponse fail:" + ex.Message);
}
if (pos != null)
{
pos.Close();
pos = null;
}
if (req != null)
{
req = null;
}
return html;
}
函数调用
UploadCSVFile(myak, mygeotable_id.ToString(), "e:\\databox_49612.csv");
相同url、文件,利用GOOGLE浏览器的PostMan工具可以上传,不知道C#里面还需要什么设置吗?
在PostMan将url后面部分修改,也会返回“访问出错”提示,怀疑是url没转成功,但是为什么会错误没有头绪。
顶一下 未知
页:
[1]