将Postman代码转为常规C#代码:Postman可登录VS中失败求助
Fixing RestSharp Login Issue for Digikala Seller Portal
Let's break down why your VS code isn't working even though Postman does, and fix it step by step.
Key Issues in Your Original Code
Your current code is missing critical pieces that Postman handles automatically:
- No login credentials (username/password, plus any required hidden fields like CSRF tokens)
- A Postman-specific header that's unnecessary (and might cause issues)
- No cookie handling to maintain the login session
Corrected RestSharp Code (Older Versions)
Here's a working version that addresses all these gaps:
using RestSharp; using System.Net; // Initialize the client with the login endpoint var client = new RestClient("https://seller.digikala.com/Account/Login"); // Enable automatic cookie management to keep the login session alive client.CookieContainer = new CookieContainer(); var request = new RestRequest(Method.POST); // Keep necessary headers - remove the Postman-specific token request.AddHeader("cache-control", "no-cache"); request.AddHeader("content-type", "application/x-www-form-urlencoded"); // Add a realistic User-Agent (some servers block requests without this) request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"); // Add ALL login parameters from Postman's Body tab // Replace these with your actual credentials and any hidden fields (like CSRF tokens) request.AddParameter("UserName", "your_seller_username"); request.AddParameter("Password", "your_seller_password"); // Example: If Postman shows a __RequestVerificationToken, add it here: // request.AddParameter("__RequestVerificationToken", "your_csrf_token_value"); // Execute the request IRestResponse response = client.Execute(request); // Check the result if (response.IsSuccessful) { Console.WriteLine("Login successful!"); // You can now use this client for subsequent authenticated requests } else { Console.WriteLine($"Login failed: {response.StatusCode}"); Console.WriteLine("Response content:"); Console.WriteLine(response.Content); }
For RestSharp v107+ (Modern API)
If you're using the latest RestSharp version, the syntax has changed slightly:
using RestSharp; using System.Net; var clientOptions = new RestClientOptions("https://seller.digikala.com") { CookieContainer = new CookieContainer() // Auto-handle session cookies }; var client = new RestClient(clientOptions); var request = new RestRequest("/Account/Login", Method.Post); request.AddHeader("content-type", "application/x-www-form-urlencoded"); request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"); // Add all required login parameters from Postman request.AddParameter("UserName", "your_seller_username"); request.AddParameter("Password", "your_seller_password"); // Add any hidden fields you see in Postman's Body tab var response = await client.ExecuteAsync(request); if (response.IsSuccessStatusCode) { Console.WriteLine("Login successful!"); } else { Console.WriteLine($"Login failed: {response.StatusCode}"); Console.WriteLine(response.Content); }
Critical Tips to Ensure Success
- Copy All Parameters from Postman:
Open Postman, go to the Body tab (underx-www-form-urlencoded), and copy every key-value pair into your code withAddParameter. This includes hidden fields like CSRF tokens (__RequestVerificationToken) which are often required for login endpoints. - Remove Postman-Specific Headers:
Thepostman-tokenheader is only for Postman's internal tracking—servers don't expect it, so removing it prevents unnecessary errors. - Check for Additional Headers:
In Postman's Headers tab, look for any other headers that are sent (likeRefererorAccept). Add them to your request if they exist. - Use Postman's Code Generator:
In Postman, click the Code button (top-right of the request builder), selectC# (RestSharp)—this will generate the exact code Postman uses, which you can compare to your own to spot missing pieces.
内容的提问来源于stack exchange,提问作者Salar Afshar
相关产品推荐
相关产品推荐

