我正在为API集成开发控制台应用程序。这是在第一个请求中获取令牌,然后在传递令牌后在第二个请求中获取报告。
string token = GetToken(app_id); // API call, which is working fine and getting token
string reportquerystring = "path?token=" + token;
WebRequest req = WebRequest.Create(@reportquerystring);
req.Method = "GET";
req.Headers["Authorization"] = "Basic " +
Convert.ToBase64String(Encoding.Default.GetBytes("username:password"));
var resp = req.GetResponse() as HttpWebResponse;
using (Stream downloadstream = resp.GetResponseStream())
{
XmlDocument reportxml = new XmlDocument();
string filename = "location\\";
string reportxmlString = (new StreamReader(downloadstream)).ReadToEnd();
reportxml.LoadXml(reportxmlString);
string json = JsonConvert.SerializeXmlNode(reportxml);
System.IO.File.WriteAllText(filename + "data_" + app_id + ".txt", json);
}
这里当我在调试时运行此代码时,在第一次调用下载报告时,响应xml为空,当我在调用之前再次拖动调试器时,在同一次运行中,它会得到正确的响应。但除非我弄清楚为什么第一次调用下载报告API不起作用或我如何使其工作,我无法继续。
有什么建议吗?