【发布时间】:2018-01-25 15:29:14
【问题描述】:
这是我的代码。 现在,当我运行代码时,从 Console.WriteLine 到终端的输出是正确的,并且是我想要写入 csv 文件的内容。但是,当我检查计算机上的 example.csv 文件时,它只有登录页面的 html,表明我已注销。这里有什么问题?
据我了解,使用 cookiejar 可以让我存储登录信息,即使有额外的请求也应该让我保持登录状态。
using System;
using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Extensions;
namespace Updater
{
class ScheduledBilling
{
public static void report()
{
var client = new RestClient("http://example.com");
client.CookieContainer = new System.Net.CookieContainer();
client.Authenticator = new SimpleAuthenticator("username", "xxx", "password", "xxx");
var request = new RestRequest("/login", Method.POST);
client.DownloadData(new RestRequest("/file", Method.GET)).SaveAs("example.csv");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}
}
class MainClass
{
public static void Main(string[] args)
{
string test = "\r\n";
ScheduledBilling.report();
Console.Write(test);
}
}
}
还有另一个相关的小问题。为什么响应引用原始登录请求时,它会在终端执行并在client.DownloadData中产生新的休息请求?
【问题讨论】: