RESTSHARP帖子问题
因此,基本上,我开始使用RestSharp学习HTTP请求,老实说,这完全是混乱的。
因此,我设法登录了我的reddit帐户,捕获upvote post请求,然后尝试发布该方法,它实际上在活动会话中起作用。(yayy一周后...)
这是我的代码:
var client = new RestClient("https://www.reddit.com/")
CookieContainer cookieContainer = new CookieContainer();
var options = new RestClientOptions()
{
MaxTimeout = -1
};
var request = new RestRequest("https://oauth.reddit.com/api/vote?redditWebClient=desktop2x&app=desktop2x-client-production&raw_json=1&gilding_detail=1", Method.Post);
request.AddHeader("accept", "*/*");
request.AddHeader("accept-language", "en-US,en;q=0.9");
request.AddHeader("authorization", "Bearer 1909307768910-Y9YshL45Wi1uTiaXGIDzIYS1CvawRw");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("origin", "https://www.reddit.com");
request.AddHeader("referer", "https://www.reddit.com/");
request.AddHeader("sec-ch-ua", "\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"");
request.AddHeader("sec-ch-ua-mobile", "?0");
request.AddHeader("sec-ch-ua-platform", "\"Windows\"");
request.AddHeader("sec-fetch-dest", "empty");
request.AddHeader("sec-fetch-mode", "cors");
request.AddHeader("sec-fetch-site", "same-site");
request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");
request.AddHeader("x-reddit-loid", "0000000000od4gjr66.2.1654778423000.Z0FBQUFBQmlzX05MUTZyNUxqOW4wcnNLNzhiNjFWeG5kR21saVptZXl6RllTcmVuMTJLV2hneEVBU3Z6SDB0VUJOQzJORzE3d0R2aWRHT1YxWFN2akdmbjg4T2Z2RVBUaWR4VjNHRk1paEFaUU9HblQydDBIbXFXZjY3WDAyaS10dGRRaDU0aWFoS2I");
request.AddHeader("x-reddit-session", "infdgnamhcklpjejeb.0.1656153357171.Z0FBQUFBQml0dVVORWozUVZwQ2RqTGdMc3MwbGJQczJvWW1UVUdmdjVHVjJqMElFV2ZwUlhOaGxmSlh0Wm02aDZsWmx2a0NtVXJKNHEyRG1Hc25tQ0FKMVdlbGlPcDN3aGVXdS1xaXJJRU9KMGVKRFQ1ZXJHTEFzSXpqSm1RUmFIa0dCNFBjU2tnYlo");
request.AddHeader("Cookie", "csv=2; edgebucket=EzrHckI9fWuza20bDr; loid=0000000000p5fofqq6.2.1656153609022.Z0FBQUFBQml0dVlKM2FDVUo5NUN2eEZBRDk4NDV5WWtDNk1mU01vRmktRnB1eWYwMzY4X3ZCdkx1eXZUNERzTVN1MkhDZXdnZFpHblMwU0k4LTc4M1VnNGRkQ3hOQUp3dTVxaFZYbndFQ19WN3NUaUFsX21MaE1SNTRaQWRsQU1sZFBUTDJQWlBoRnM; session_tracker=zoCNAuHADuPm2Hy4AF.0.1656153616293.Z0FBQUFBQml0dVlRRTZCc2NBNHRmV2xxUWhwVFllT0JaUGhDN3JDNTYxRk1DLURsTmJ4U1ZVOUNLRjN4RjZHNExpZUZjcVd6MzktMGVZMS12X193SDJ2OUd0R19kVjJ3dEtnWGFPOEVHSWd5dHBIWFVZcWNReEZPb0Q5UG9mRVlyWUFXOC1lMEYxZ20");
request.AddParameter("id", "t3_qbl6b2");
request.AddParameter("id", "t3_vk7gmb");
request.AddParameter("dir", "1");
request.AddParameter("api_type", "json");
RestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
好的,这里是IM对官方的Reddit API或任何官方API并不感兴趣,因为我只是想学习,而且我没有精确的目标。我有一些问题需要精确。我在YouTube上找到了一些解释,但在这里或Google并不清楚,但我看到RestSharp最近已更新。
1-到目前为止,我了解的是,我可以做这个帖子请求,因为主动会话正在使用相同的携带者和cookie,但是我想每次都会拦截帖子请求以生成这些数据,因此您如何实际获得会话在实际执行请求之前,ID/LOID/COOKIE/BEARER和所有您需要提出成功请求的动态数据(逻辑大声笑)?
2-如何处理饼干?
3-您需要在发布之前提出GET请求吗?
4-如何处理响应中的对象和数据?
我想学习,所以如果你们有一些知识要分享,这将不胜感激!
so basically i started learning http request with restsharp and to be honest there is a total confusion.
So i managed to log into my reddit account, catch upvote post request and i tried to post the method and it actually worked on the active session.(yayy after a week...)
Here is my code:
var client = new RestClient("https://www.reddit.com/")
CookieContainer cookieContainer = new CookieContainer();
var options = new RestClientOptions()
{
MaxTimeout = -1
};
var request = new RestRequest("https://oauth.reddit.com/api/vote?redditWebClient=desktop2x&app=desktop2x-client-production&raw_json=1&gilding_detail=1", Method.Post);
request.AddHeader("accept", "*/*");
request.AddHeader("accept-language", "en-US,en;q=0.9");
request.AddHeader("authorization", "Bearer 1909307768910-Y9YshL45Wi1uTiaXGIDzIYS1CvawRw");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("origin", "https://www.reddit.com");
request.AddHeader("referer", "https://www.reddit.com/");
request.AddHeader("sec-ch-ua", "\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"");
request.AddHeader("sec-ch-ua-mobile", "?0");
request.AddHeader("sec-ch-ua-platform", "\"Windows\"");
request.AddHeader("sec-fetch-dest", "empty");
request.AddHeader("sec-fetch-mode", "cors");
request.AddHeader("sec-fetch-site", "same-site");
request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");
request.AddHeader("x-reddit-loid", "0000000000od4gjr66.2.1654778423000.Z0FBQUFBQmlzX05MUTZyNUxqOW4wcnNLNzhiNjFWeG5kR21saVptZXl6RllTcmVuMTJLV2hneEVBU3Z6SDB0VUJOQzJORzE3d0R2aWRHT1YxWFN2akdmbjg4T2Z2RVBUaWR4VjNHRk1paEFaUU9HblQydDBIbXFXZjY3WDAyaS10dGRRaDU0aWFoS2I");
request.AddHeader("x-reddit-session", "infdgnamhcklpjejeb.0.1656153357171.Z0FBQUFBQml0dVVORWozUVZwQ2RqTGdMc3MwbGJQczJvWW1UVUdmdjVHVjJqMElFV2ZwUlhOaGxmSlh0Wm02aDZsWmx2a0NtVXJKNHEyRG1Hc25tQ0FKMVdlbGlPcDN3aGVXdS1xaXJJRU9KMGVKRFQ1ZXJHTEFzSXpqSm1RUmFIa0dCNFBjU2tnYlo");
request.AddHeader("Cookie", "csv=2; edgebucket=EzrHckI9fWuza20bDr; loid=0000000000p5fofqq6.2.1656153609022.Z0FBQUFBQml0dVlKM2FDVUo5NUN2eEZBRDk4NDV5WWtDNk1mU01vRmktRnB1eWYwMzY4X3ZCdkx1eXZUNERzTVN1MkhDZXdnZFpHblMwU0k4LTc4M1VnNGRkQ3hOQUp3dTVxaFZYbndFQ19WN3NUaUFsX21MaE1SNTRaQWRsQU1sZFBUTDJQWlBoRnM; session_tracker=zoCNAuHADuPm2Hy4AF.0.1656153616293.Z0FBQUFBQml0dVlRRTZCc2NBNHRmV2xxUWhwVFllT0JaUGhDN3JDNTYxRk1DLURsTmJ4U1ZVOUNLRjN4RjZHNExpZUZjcVd6MzktMGVZMS12X193SDJ2OUd0R19kVjJ3dEtnWGFPOEVHSWd5dHBIWFVZcWNReEZPb0Q5UG9mRVlyWUFXOC1lMEYxZ20");
request.AddParameter("id", "t3_qbl6b2");
request.AddParameter("id", "t3_vk7gmb");
request.AddParameter("dir", "1");
request.AddParameter("api_type", "json");
RestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Ok the thing here is im not really interested into official reddit api or any official api as im only trying to learn and i have no precise goal. I have some questions that i need to be precised. I found some explanations on youtube, here or google but its not really clear and i seen that restsharp have updated recently.
1- So far what i understand, its that i can do this post request because the active session is using the same Bearer and cookies but i guess intercepting the post request each time to generate these data aint really practical so how you actually get the session id/loid/cookies/bearer and all dynamic data that you need to make succesful request before actually doing the request(logic lol)?
2- How to handle cookies?
3- Do you need to make a get request before a post?
4- How to handle objects and data from the response?
I want to learn so if you guys have some knowledge to share, it will be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这完全无关。
- 因此,基本上,您需要提出一个将提供HTML代码的GET请求。
- 在此HTML代码中,您会找到一个CSRF令牌。您将使用Regex将其放入字符串中。
- 您将创建一个返回用户名,密码和CSRF代币的POST登录请求。
您现在已记录!!!
关于处理cookie,只要保留相同的retclient对象,RestSharp就会自动进行。我将它们放在字符串数组中,然后使用string.format在请求中使用它们
Ok so it was totally not about oauth at all.
-So basically, you need to make a GET request that will deliver an html code.
-In this html code you will find an csrf token. You will use regex to put it in a string.
-You will create a POST login request that return username, password and csrf token.
You're now logged!!!
About handling cookies, restsharp do it automatically as long as you keep the same RestClient object. I put them in a string array and use them in the request using String.Format