C#(.NET),带有Auth的http Web请求(发布方法).和饼干 [英] C# (.NET), http web request (post method) with Auth. and Cookies

查看:459
本文介绍了C#(.NET),带有Auth的http Web请求(发布方法).和饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#应用程序在站点操作皮肤中按一个按钮.当我在浏览器中按它时,提琴手接下来给我

I'm trying to press one button in site opskins with C# app. When I press on it in browser, fiddler give me next

我没有足够的经验,所以我认为我添加了多余的标题,犯了愚蠢的错误等等……

I don't have enough experience for that, so I think I add excess headers, make stupid mistakes, etc...

我的代码:

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);
}  

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

When my app is running, fiddler give me

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

But still not working, what i must fix or add to my code? Someone can tell me easier way to press one button in my site (only C#) ?

推荐答案

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

According to Fiddler, the url in your request constructor should be "https://opskins.com/ajax/shop_buy_item.php";

尝试使用CookieContainer存放cookie并在请求中添加要发送的每个cookie.

Try to use CookieContainer to stash your cookies and add each of your cookies you want to send in the request.

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

还添加此标头:request.Referer = referer

如果仍然无法解决问题,我建议将第一个Fiddler请求中的其余标头添加到您的Web请求中.

If that still doesn't work, I'd recommend adding the rest of the headers from the first Fiddler request into your web request.

这篇关于C#(.NET),带有Auth的http Web请求(发布方法).和饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆