我正在尝试使用this page作为参考,通过HTTP帖子从Datacash下载报告,我无法弄清楚我哪里出错了。
我的方法如下:
private static void DownloadDatacashData(int howManyDays)
{
string post, url, group, user, password, startDate, endDate, type, csvFile;
url = "https://reporting.datacash.com/reporting2/csvlist?";
group = "123456";
user = "autoreport";
password = "foobar";
startDate = DateTime.Now.AddDays(howManyDays).ToString("yyyy-MM-dd");
endDate = DateTime.Now.ToString("yyyy-MM-dd");
type = "stl";
post = String.Format(@"group={0}&user={1}&password={2}&start_date={3}&end_date={4}&type={5}", group, user, password, startDate, endDate, type);
var request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version11;
request.Method = "POST";
request.ContentLength = 0;
var postBytes = Encoding.ASCII.GetBytes(post);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
var requestStream = request.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
var response = (HttpWebResponse)request.GetResponse();
var sr = new StreamReader(response.GetResponseStream());
csvFile = sr.ReadToEnd();
sr.Close();
var sw = new StreamWriter("C:\\foo\\bar.csv", false);
sw.Write(csvFile);
sw.Flush();
sw.Close();
}
首先,发布帖子。但我总是得到一个文件“Error:Report type not found”。如果我更改了URL,则会收到错误,因此该URL肯定会响应HTTP帖子。我有没有试过这个?在初始URL中,有和没有?在帖子字符串的开头。
我已经尝试与Datacash的技术支持取得联系,他们无法帮助我。我向他们发送了我正在使用的代码,而我所得到的只是他们的结果没有任何问题。
凭据正确,密码是字母和数字的组合(没有特殊字符)。
我知道你们中没有多少人会使用datacash但是查看我的代码和信息here,我正在做的事情有什么明显的错误吗?我四处走动。
三江源
答案 0 :(得分:9)
对于像这样的事情,你的代码似乎太复杂了。尝试简化:
private static void DownloadDatacashData(int howManyDays)
{
var url = "https://reporting.datacash.com/reporting2/csvlist";
using (var client = new WebClient())
{
var values = new NameValueCollection
{
{ "group", "123456" },
{ "user", "autoreport" },
{ "password", "foobar" },
{ "start_date", DateTime.Now.AddDays(howManyDays).ToString("yyyy-MM-dd") },
{ "end_date", DateTime.Now.ToString("yyyy-MM-dd") },
{ "type", "stl" },
};
byte[] result = client.UploadValues(url, values);
File.WriteAllBytes("C:\\foo\\bar.csv", result);
}
}