我正在做一个浏览器游戏的测试。这个帖子请求是为了发出一个命令来建造一个建筑物。只要我登录了,我就可以点击 Postman 发送按钮,它就能正常工作。这意味着没有一个值会过期。但是如果我注销并重新登录,它就不能工作了。我必须通过interceptor
生成另一个 Postman 帖子。
我想用C#模拟这个post请求;
下面是我的帖子在Postman中的样子;
Postman 中的邮件头x1c 0d1x
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#语言以编程方式工作,就没有规则。
我希望看到以以下内容开头的响应:
{“成功”:“完成日期”:86,“完成日期格式”:“0:01:26”,“构建订单”:“
**编辑:**我可以使用其他方法来模拟此帖子请求。它不必在RestSharp中。
**编辑2:**我将在每次登录时更新我得参数,以便您可以重现我遇到得问题.
**编辑3:**Postman能够使用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参数放入生成的代码中。
1条答案
按热度按时间dojqjjoe1#
感谢CodeCaster和他使用Fiddler的建议,我能够使用
HttpWebRequest
发出请求。有一个惊人的Fiddler tool将请求转换为C#代码。这就是我解决这个问题的方法