RestClient 有效,但 HttpWebRequest 无效
RestClient works but HttpWebRequest doesn't
此代码有效:
var source = "https://jade.io/xml/au-qld-dc.xml";
var client = new RestClient(source);
var request = new RestRequest(Method.GET);
IRestResponse resp = client.Execute(request);
Console.WriteLine(resp.Content);
xml 被检索并显示在控制台中。
但是这段代码不起作用:
HttpWebRequest httpsRequest = (HttpWebRequest) WebRequest.Create(source);
httpsRequest.Method = "GET";
var response = httpsRequest.GetResponse();
它抛出 403(禁止)错误...
我想知道为什么它不起作用,因为我有一些遗留代码使用 WebRequest,在用 RestClient 替换所有这些代码之前,如果有一个简单的修复...
添加 UserAgent header 它会起作用。
var source = "https://jade.io/xml/au-qld-dc.xml";
HttpWebRequest httpsRequest = (HttpWebRequest)WebRequest.Create(source);
httpsRequest.Method = "GET";
httpsRequest.UserAgent = "Test";
var response = httpsRequest.GetResponse();
此代码有效:
var source = "https://jade.io/xml/au-qld-dc.xml";
var client = new RestClient(source);
var request = new RestRequest(Method.GET);
IRestResponse resp = client.Execute(request);
Console.WriteLine(resp.Content);
xml 被检索并显示在控制台中。 但是这段代码不起作用:
HttpWebRequest httpsRequest = (HttpWebRequest) WebRequest.Create(source);
httpsRequest.Method = "GET";
var response = httpsRequest.GetResponse();
它抛出 403(禁止)错误...
我想知道为什么它不起作用,因为我有一些遗留代码使用 WebRequest,在用 RestClient 替换所有这些代码之前,如果有一个简单的修复...
添加 UserAgent header 它会起作用。
var source = "https://jade.io/xml/au-qld-dc.xml";
HttpWebRequest httpsRequest = (HttpWebRequest)WebRequest.Create(source);
httpsRequest.Method = "GET";
httpsRequest.UserAgent = "Test";
var response = httpsRequest.GetResponse();