什么是送在C#中使用HttpWebRequest的一个“AJAX”POST请求的正确方法? [英] What is the correct way to send an 'ajax' post request using httpwebrequest in C#?
问题描述
下面的请求头从萤火虫产生的点击网页上的按钮(网址不会改变,所以它是一个ajax post请求!)。我想重复使用的HttpWebRequest在我的C#程序申请。
The following request-header is generated from firebug on clicking a button on a webpage(the url doesn't change, so it is an ajax post request!). I would like to replicate this request using httpwebrequest in my C# program.
Accept :text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding :gzip, deflate
Accept-Language :en-US,en;q=0.5
Cache-Control :no-cache
Connection :keep-alive
Content-Length :725
Content-Type :application/x-www-form-urlencoded;charset=UTF-8
Cookie :language=en_IN; _ga=GA1.3.184236920.1443720063;__gads=ID=9a55686039c6e6f0:T=1443720047:S=ALNI_MaWPaHUEUBgf-Zf7qdtCfl0jdtTcQ; JSESSIONID=nnMsc6NkqzXSgHXQ0DaNuHLwMl0OLChvEiv2hxBtunG22tmF_rTB!-30889781; SLB_Cookie=ffffffff09461c2745525d5f4f58455e445a4a422972; _gat_UA-57718223-1=1; _gat_UA-57718223-3=1
Faces-Request :partial/ajax
Host :www.example.com
Pragma :no-cache
Referer :https://www.example.com/photography/homepage.jsf
User-Agent :Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
这是我试过到目前为止,但它导致错误412服务器。我已经确定了post_data字符串是正确的,但它仍然是行不通的。
This is what I have tried so far but it results in Error 412 from the server. I have made sure that the post_data string is correct, but still it isn't working.
string post_data= "inputexForm=inputexForm&cat=senior&inputexForm%3aid=4174622&...addtab&AJAX%3AEVENTS_COUNT=1&javax.faces.partial.ajax=true";
httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.example.com/photography/homepage.jsf");
httpWebRequest.CookieContainer = cookieJar;
httpWebRequest.Referer = "https://www.example.com/photography/homepage.jsf";
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0";
httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
httpWebRequest.KeepAlive = true;
httpWebRequest.Connection = "keepalive";
byte[] data = UTF8Encoding.UTF8.GetBytes(post_data);
httpWebRequest.ContentLength = data.Length;
using (var stream = httpWebRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
stream.Close();
}
HttpWebResponse resp = (HttpWebResponse)httpWebRequest.GetResponse();
webBrowser1.DocumentText = resp.ToString();
我
如何发送这个AJAX POST请求到服务器在C#中使用HttpWebRequest的?
How can I send this ajax post request to the server using HttpWebRequest in C#?
推荐答案
要检测一个请求是AJAX,服务器会检查是否存在一个标头键X-要求,随着中
值为XMLHtt prequest
。所以,你可以使用 HttpWebRequest.Headers
属性添加此键值对您的请求头:
To detect if a request is ajax, a server checks existence of a header key "X-Requested-With"
with value "XMLHttpRequest"
. So you can add this key-value pair to your request header using HttpWebRequest.Headers
property:
var request = (HttpWebRequest)WebRequest.Create("target url");
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
这篇关于什么是送在C#中使用HttpWebRequest的一个“AJAX”POST请求的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!