如何正确地将POST请求中的参数传递给WebRequest?

时间:2018-02-09 10:43:50

标签: c# asp.net

我尝试在授权表单上创建请求者,如果数据正确 - 将重定向到另一个页面,如果不正确 - 将返回授权表单。

WebRequest request = WebRequest.Create ("http://www.request.com/");
request.Proxy = new WebProxy (new Uri ("http://myproxy.ru"));
request.Method = "POST";
request.ContentType = "application / x-www-form-urlencoded";

NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString (String.Empty);

outgoingQueryString.Add ("_method", "POST");
outgoingQueryString.Add ("data [User] [email]", "email@my.ru");
outgoingQueryString.Add ("data [User] [password]", "QWErty01");

byte [] byteArray = new ASCIIEncoding (). GetBytes (outgoingQueryString.ToString ());

request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();
WebResponse response = request.GetResponse ();

dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader (dataStream);
string responseFromServer = reader.ReadToEnd ();

reader.Close ();
dataStream.Close ();
response.Close ();

在responseFromServer中,我确切地找到了授权的形式,就像数据不正确一样。

同时,为

处理相同的JS请求
$.ajax({
  type: "POST",
  url: 'http://cora-request.com/',
  data: { '_method': 'POST', "data[User][email]": "email@my.ru", "data[User][password]": "QWErty01" },
  success: function(response){ console.log(response) }
});

可能是什么原因?

0 个答案:

没有答案