C# http请求带请求头部分
使用js+Ajax请求API接口数据-带请求头方式
直接上代码
前台调用:
后台部分:
////// 后台再请求别的接口的方法,体现了添加request header /// /// public string Login(string RequestString, HttpContext context) { string url = https://xxx.xxxx.com/Services/Login.ashx; string json = ""; Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "post"; request.Headers.Add("Method", context.Request.Headers["Method"]); request.Headers.Add("timespan", context.Request.Headers["Timespan"]); request.Headers.Add("Nonce", context.Request.Headers["Nonce"]); request.Headers.Add("Token", context.Request.Headers["Token"]); request.Headers.Add("Account", System.Web.HttpUtility.UrlDecode(context.Request.Headers["Account"])); request.Headers.Add("Source", context.Request.Headers["Source"]); request.Headers.Add("Version", context.Request.Headers["Version"]); request.Headers.Add("Sign", context.Request.Headers["Sign"]); request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/x-www-form-urlencoded"; byte[] buffer = encoding.GetBytes(RequestString.ToString()); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { json = reader.ReadToEnd(); } return json; }