通过HTTP POST下载的文件,我要去哪里错了? [英] Downloading a file via an HTTP Post, where am I going wrong?
问题描述
我试图通过使用的此页面作为参考,我想不通我要去的地方错了。
I am trying to download a report from Datacash via an HTTP post using this page as a reference and I can't figure out where I am going wrong.
我的。方法如下:
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();
}
首先,执行岗位。但是,我总是得到一个文件回来文本错误:未找到报表类型。如果我更改URL,我得到一个错误,所以这个URL绝对是响应HTTP职位。我曾试图与和没有?在初始的URL,和有和没有?在后字符串的开始。
First of all, the post is performed. But I always get a file back with the text "Error: Report type not found". If I change the URL, I get an error so this URL is definitely responding to HTTP post. I have tried this with and without the ? in the initial URL, and with and without the ? at the start of the post string.
我已经尝试与Datacash的技术支持取得联系,他们不能帮助我。我给他们送去我使用的代码和所有我回来是,有没有什么错在他们的结局。
I have already tried to get in touch with Datacash's technical support and they couldn't help me. I sent them the code I am using and all I got back was that there wasn't anything wrong on their end.
的凭据是正确的,密码是一个组合字母和数字(无特殊字符)。
The credentials are correct, the password is a combination of letters and numbers (no special characters).
我知道这不是你们中许多人将已经使用datacash但是看着我的代码和信息的这里,有什么明显错误?与我在做什么?我兜兜转转。
I know that not many of you will have used datacash but looking at my code and the information here, is there anything obviously wrong with what I am doing? I am going around in circles.
三江源
推荐答案
您的代码似乎太复杂了这样的事情。尽量简化:
Your code seems way too complicated for something like this. Try simplifying:
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);
}
}
这篇关于通过HTTP POST下载的文件,我要去哪里错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!