在 C# 中模拟 Postman Post - RestSharp

Simulate Postman Post in C# - RestSharp

我正在对网页游戏进行测试。此 post 请求假设发出建造建筑物的命令。只要我登录了,我就可以点击 Postman Send 按钮,它工作正常。这意味着没有值过期。但是,如果我注销并重新登录,它将无法正常工作。我将不得不通过 interceptor 生成另一个 Postman post。

我想使用 C# 模拟此 post 请求;

这是我的 post 在 Postman;

中的样子

Headers 在 Post 人

Body参数在Postman

如果您安装了 postman,请单击 here 导入请求。

这是我的 C# 代码。我正在使用名为 RestSharp;

的库
var client = new RestClient(@"https://tr42.klanlar.org");
var request = new RestRequest("game.php", Method.POST);
request.AddQueryParameter("village", "31413"); // adds to POST or URL querystring based on Method
request.AddQueryParameter("screen", "main"); // adds to POST or URL querystring based on Method
request.AddQueryParameter("ajaxaction", "upgrade_building"); // adds to POST or URL querystring based on Method
request.AddQueryParameter("type", "main"); // adds to POST or URL querystring based on Method
request.AddQueryParameter("h", "98e34aa6"); // adds to POST or URL querystring based on Method
request.AddQueryParameter("", ""); // adds to POST or URL querystring based on Method
request.AddQueryParameter("client_time", "1505659914"); // adds to POST or URL querystring based on Method

request.AddParameter("destroy", "0"); // adds to POST or URL querystring based on Method
request.AddParameter("force", "1"); // adds to POST or URL querystring based on Method
request.AddParameter("id", "wood"); // adds to POST or URL querystring based on Method
request.AddParameter("source", "31413"); // adds to POST or URL querystring based on Method

//var p1 = new Parameter
//{
//    ContentType = "application/x-www-form-urlencoded",
//    Name = "destroy",
//    Value = "0"
//};
//request.AddParameter(p1);

//var p2 = new Parameter
//{
//    ContentType = "application/x-www-form-urlencoded",
//    Name = "force",
//    Value = "1"
//};
//request.AddParameter(p2);

//var p3 = new Parameter
//{
//    ContentType = "application/x-www-form-urlencoded",
//    Name = "id",
//    Value = "wood"
//};
//request.AddParameter(p3);

//var p4 = new Parameter
//{
//    ContentType = "application/x-www-form-urlencoded",
//    Name = "source",
//    Value = "31413"
//};
//request.AddParameter(p4);


request.AddHeader("Accept-Language", "en-US,en;q=0.8,tr;q=0.6,ht;q=0.4");
request.AddHeader("Accept-Encoding", "gzip, deflate, br");
request.AddHeader("Referer", "https://tr42.klanlar.org/game.php?village=31413&screen=main");
//request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Tribalwars-Ajax", "1");
request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36");
request.AddHeader("X-Requested-With", "XMLHttpRequest");
request.AddHeader("Origin", "https://tr42.klanlar.org");
request.AddHeader("Accept", "application/json, text/javascript, */*; q=0.01");


//request.AddParameter("application/x-www-form-urlencoded", "destroy=&force=&id=&source=");
//request.AddParameter("application/x-www-form-urlencoded", "destroy=0&force=1&id=31413&source=wood", ParameterType.RequestBody);
//request.AddBody("application/x-www-form-urlencoded", "destroy=0&force=1&id=31413&source=wood");
IRestResponse response = client.Execute(request);
var data = response.Content;

当我单击 Postman 中的 Send 按钮时,该操作有效。然而,即使我尝试了很多不同的方法,我也无法让我的 C# 函数正常工作。

允许使用任何类型的图书馆肮脏的把戏等等。没有规则,只要请求使用 C# 语言以编程方式工作即可。

我期待看到以;

开头的回复

{"success":"Komut verildi.","date_complete":86,"date_complete_formated":"0:01:26","building_orders":"

编辑: 我可以使用替代方法来模拟此 Post 请求。它不必在 RestSharp 中。

编辑 2: 我会在每次登录时更新我的​​参数,以便您可以重现我遇到的问题。

编辑 3: Post人能够使用 RestSharp 生成 C# 代码。这是 Postman 生成的代码。但是它不起作用。

var client = new RestClient("https://tr42.klanlar.org/game.php?village=31413&screen=main&ajaxaction=upgrade_building&type=main&h=8951b405&=&client_time=1505062595");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "dab31db4-b243-c317-e585-19da84c11e62");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("accept-language", "en-US,en;q=0.8,tr;q=0.6,ht;q=0.4");
request.AddHeader("accept-encoding", "gzip, deflate, br");
request.AddHeader("referer", "https://tr42.klanlar.org/game.php?village=31413&screen=main");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("tribalwars-ajax", "1");
request.AddHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36");
request.AddHeader("x-requested-with", "XMLHttpRequest");
request.AddHeader("origin", "https://tr42.klanlar.org");
request.AddHeader("accept", "application/json, text/javascript, */*; q=0.01");
IRestResponse response = client.Execute(request);

我注意到 Postman 没有将 body 参数放入生成的代码中。

感谢 CodeCaster with his advice to use Fiddler, I was able to make the request using HttpWebRequest. There is this amazing Fiddler tool 将请求转换为 C# 代码。

这就是我解决这个问题的方法;

  • 已安装 Fiddler
  • 使用 fiddler
  • 捕获了 post 请求
  • 已安装 this fiddler 扩展
  • 将 fiddler 请求转换为如下所示的 C# 代码;

    private void MakeRequests()
    {
        HttpWebResponse response;
    
        if (Request_tr42_klanlar_org(out response))
        {
            response.Close();
        }
    }
    
    private bool Request_tr42_klanlar_org(out HttpWebResponse response)
    {
        response = null;
    
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://tr42.klanlar.org/game.php?village=31413&screen=main&ajaxaction=upgrade_building&type=main&h=561d76fe&&client_time=1505677450");
    
            request.KeepAlive = true;
            request.Accept = "application/json, text/javascript, */*; q=0.01";
            request.Headers.Add("Origin", @"https://tr42.klanlar.org");
            request.Headers.Add("X-Requested-With", @"XMLHttpRequest");
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";
            request.Headers.Add("TribalWars-Ajax", @"1");
            request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            request.Referer = "https://tr42.klanlar.org/game.php?village=31413&screen=main";
            request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
            request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.8,tr;q=0.6,ht;q=0.4");
            request.Headers.Set(HttpRequestHeader.Cookie, @"cid=1584941605; tr_auth=e341b46da1e7:13bc09020be8c378853c70baaa54ed9849c029468a62a143d10868e4238fa1af; cid=1584941605; tr_auth=e341b46da1e7:13bc09020be8c378853c70baaa54ed9849c029468a62a143d10868e4238fa1af; ref4852238=start; _ga=GA1.2.766176105.1499529449; _gid=GA1.2.1638517063.1505659564; __utmt=1; __utma=37229925.766176105.1499529449.1505094554.1505659568.8; __utmb=37229925.10.10.1505659568; __utmc=37229925; __utmz=37229925.1505659568.8.3.utmcsr=klanlar.org|utmccn=(referral)|utmcmd=referral|utmcct=/; sid=0%3A12986160bd90; _ga=GA1.2.766176105.1499529449; _gid=GA1.2.1638517063.1505659564; __utma=37229925.766176105.1499529449.1505094554.1505659568.8; __utmb=37229925.11.10.1505659568; __utmc=37229925; __utmz=37229925.1505659568.8.3.utmcsr=klanlar.org|utmccn=(referral)|utmcmd=referral|utmcct=/; websocket_available=true; global_village_id=31413; mobile=0");
    
            request.Method = "POST";
            request.ServicePoint.Expect100Continue = false;
    
            string body = @"id=stone&force=1&destroy=0&source=31413";
            byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
            request.ContentLength = postBytes.Length;
            Stream stream = request.GetRequestStream();
            stream.Write(postBytes, 0, postBytes.Length);
            stream.Close();
    
            response = (HttpWebResponse)request.GetResponse();
        }
        catch (WebException e)
        {
            if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
            else return false;
        }
        catch (Exception)
        {
            if (response != null) response.Close();
            return false;
        }
        return true;
    }