You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将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

  1. Copy All Parameters from Postman:
    Open Postman, go to the Body tab (under x-www-form-urlencoded), and copy every key-value pair into your code with AddParameter. This includes hidden fields like CSRF tokens (__RequestVerificationToken) which are often required for login endpoints.
  2. Remove Postman-Specific Headers:
    The postman-token header is only for Postman's internal tracking—servers don't expect it, so removing it prevents unnecessary errors.
  3. Check for Additional Headers:
    In Postman's Headers tab, look for any other headers that are sent (like Referer or Accept). Add them to your request if they exist.
  4. Use Postman's Code Generator:
    In Postman, click the Code button (top-right of the request builder), select C# (RestSharp)—this will generate the exact code Postman uses, which you can compare to your own to spot missing pieces.

内容的提问来源于stack exchange,提问作者Salar Afshar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:32:18