如何在不注销的情况下使用 Restsharp 下载 CSV 文件?
How Do You Download a Csv File With Restsharp Without Getting Logged Out?
这是我的代码。
现在,当我 运行 代码时,从 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);
}
}
}
还有一个相关的小问题。为什么response指的是原来的登录请求,却在终端执行并产生client.DownloadData中新的rest请求?
您在尝试下载 CSV 文件之前没有执行登录请求。您的代码应如下所示:
public static void report()
{
//Creates the client
var client = new RestClient("http://example.com");
client.CookieContainer = new System.Net.CookieContainer();
client.Authenticator = new SimpleAuthenticator("username", "xxx", "password", "xxx");
//Creates the request
var request = new RestRequest("/login", Method.POST);
//Executes the login request
var response = client.Execute(request);
//This executes a seperate request, hence creating a new requestion
client.DownloadData(new RestRequest("/file", Method.GET)).SaveAs("example.csv");
Console.WriteLine(response.Content);
}
这是我的代码。 现在,当我 运行 代码时,从 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);
}
}
}
还有一个相关的小问题。为什么response指的是原来的登录请求,却在终端执行并产生client.DownloadData中新的rest请求?
您在尝试下载 CSV 文件之前没有执行登录请求。您的代码应如下所示:
public static void report()
{
//Creates the client
var client = new RestClient("http://example.com");
client.CookieContainer = new System.Net.CookieContainer();
client.Authenticator = new SimpleAuthenticator("username", "xxx", "password", "xxx");
//Creates the request
var request = new RestRequest("/login", Method.POST);
//Executes the login request
var response = client.Execute(request);
//This executes a seperate request, hence creating a new requestion
client.DownloadData(new RestRequest("/file", Method.GET)).SaveAs("example.csv");
Console.WriteLine(response.Content);
}