获取http请求标头值到c# [英] get http request header values in to c#
本文介绍了获取http请求标头值到c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个应用程序,用户通过单点登录进行身份验证并进入我们的系统请求我们的网页。
我如何获取/读取http请求(到我的服务器)标头值(包括用户名和密码)使用c#进入我的应用程序。
我认为它就像查询字符串而不是我想用user.header但找不到任何文档。
任何资源会有所帮助。
谢谢!
I have a an application where users are authenticated using single sign on and enter our system requesting our webpage.
How would I get/read the http request(to my server) header values(including username and password) in to my application using c#.
I think its just like querystring instead I want to user request.header but could not find any documentation.
Any resource would be helpful.
Thanks!
推荐答案
使用以下方法获取cookie ...
Use the following method to Get cookie...
private static string GetCookie()
{
string uri = string.Empty;
string cookie = string.Empty;
HttpWebResponse response = POST(uri);
if (response == null)
{
this.LoginStatus = "Not connected";
return "";
}
WebHeaderCollection headers = response.Headers;
if ((response.StatusCode == HttpStatusCode.Found) ||
(response.StatusCode == HttpStatusCode.Redirect) ||
(response.StatusCode == HttpStatusCode.Moved) ||
(response.StatusCode == HttpStatusCode.MovedPermanently))
{
if (headers["Set-Cookie"] != null)
{
string cookies = headers["Set-Cookie"];
String[] fields = Regex.Split(cookies, ";\\s*");
cookie = fields[0];
}
}
return cookie;
}
public static HttpWebResponse POST(string url)
{
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
request.Accept = "*/*";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response;
}
catch
{
return (HttpWebResponse)null;
}
}
现在您可以使用返回的cookie并使用另一个POST或GET,发布以下示例:
Now you can use the cookie returned and use it another POST or GET, post sample below:
public static HttpWebResponse POST(string postData, string url, string referer, string cookie)
{
try
{
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
request.Accept = "*/*";
request.ContentType = "application/x-www-form-urlencoded";
if (!string.IsNullOrEmpty(cookie))
request.Headers.Add(HttpRequestHeader.Cookie, cookie);
if (!string.IsNullOrEmpty(referer))
request.Referer = referer;
request.ContentLength = byteArray.Length;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
// request.Proxy = null;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
try
{
return (HttpWebResponse)request.GetResponse();
}
catch
{
return (HttpWebResponse)null;
}
}
catch
{
return (HttpWebResponse)null;
}
}
这篇关于获取http请求标头值到c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文