使用Auth的C#(.NET),http web请求(post方法)。和饼干

时间:2017-02-01 18:21:39

标签: c# .net http post request

我试图用C#app在网站opskins中按一个按钮。当我在浏览器中按下它时,小提琴手给我下一个

enter image description here

我没有足够的经验,所以我想我会添加多余的标题,犯下愚蠢的错误等......

我的代码:

string username = textBox2.Text;
string password = textBox3.Text;
string userData = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));

/*Add Auth Header*/
string referer = "https://opskins.com/?loc=shop_checkout";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(referer);

/*Cookie and spec. Headers*/
request.Headers.Add("Cookie", "Cookie string from Fiddler here");
request.Headers.Add("X-OP-UserID", "2484329");
request.Headers.Add("X-CSRF", "2kXrd2qBf4eFxW1O6tM2ye3DELn4SJzDH");

request.Headers.Add("Authorization", "Basic" + userData);
string fData = "action=buy&hidden_bal=0&total=" + textBox1.Text + "&accept_tos=1&type=2";
byte[] eData = Encoding.UTF8.GetBytes(fData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
request.ContentLength = eData.Length;

using (var stream = request.GetRequestStream())
{
stream.Write(eData, 0, eData.Length);
}  

当我的应用程序运行时,小提琴手给我

enter image description here

但仍然无法正常工作,我必须修复或添加到我的代码中? 有人可以告诉我在我的网站上按一个按钮的简单方法(只有C#)?

1 个答案:

答案 0 :(得分:0)

根据Fiddler的说法,请求构造函数中的url应为"https://opskins.com/ajax/shop_buy_item.php";

尝试使用CookieContainer存储您的Cookie并添加您要在请求中发送的每个Cookie。

request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(new URI("https://opskins.com"), new Cookie(key, value);

同时添加此标头:request.Referer = referer

如果仍然无效,我建议将第一个Fiddler请求中的其余标题添加到您的网络请求中。