我有以下一段代码让我无法阅读Request.Params
的值。现在我只想读取我从发件人传递的值(在收件人中),即用户名和SAMLResponse。
发件人
protected void Button1_Click(object sender, EventArgs e)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("MY URL");
httpWReq.Method = "Post";
XElement obj = XElement.Load(@"Load.xml");
StringBuilder postData = new StringBuilder();
postData = postData.Append("username=user&SAMLResponse=").Append(obj.ToString());
byte[] data = Encoding.UTF8.GetBytes(postData.ToString());
httpWReq.Method = "POST";
httpWReq.ContentType = "text/xml;encoding='utf-8'";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
接收方
public ActionResult LoginSSO()
{
string rawSamlData, WindowName;
SSOModel objSSOModel = new SSOModel();
string str = Request.Params["username"];
str = Request.Params["SAMLResponse"];
...
..
}
错误:
任何想法,出了什么问题?
答案 0 :(得分:0)
你可以改变这一行:
httpWReq.ContentType = "text/xml;encoding='utf-8'";
到此:
httpWReq.ContentType = "application/x-www-form-urlencoded";
给我们一个结果?
https://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx
答案 1 :(得分:0)
我发现您的代码存在一些问题。
obj.ToString()
时对XML文件进行编码,因为它可能包含会混淆事物的字符。application/x-www-form-urlencoded
答案 2 :(得分:0)
这适用于我的应用程序。
正如其他回答者所说,在发件人中将内容类型更改为"application/x-www-form-urlencoded"
。
在接收器中将string str = Request.Params["username"];
更改为string str = Request.Form["username"];
或string str = Request["username"];
。
我希望他们也能在您的申请中使用。