• C# http请求带请求头部分


    前台调用:

    复制代码
    <script type="text/javascript">
            function zLoginCheck() {
                var Account = 'admin;
                var Password = 'DC483E80A7A0BD9EF71D8CF973673924';
    
                var str = {
                    Account: Account,
                    Password: Password
                }
                $.ajax({
                    type: "POST",
                    url: '/Handle/zLogin.ashx',
                    dataType: 'json',
                    data: JSON.stringify(str),//
                    beforeSend: function (request) {
                        request.setRequestHeader("Method", "Login");
                        request.setRequestHeader("timespan", "1487586970772");
                        request.setRequestHeader("Nonce", "726757");
                        request.setRequestHeader("Token", "615d1c0ab53315a2");
                        request.setRequestHeader("Account", "");
                        request.setRequestHeader("Source", "2");
                        request.setRequestHeader("Version", "139");
                        request.setRequestHeader("Sign", "5329E35A3F60B7DFA147D20EF377888C");
                        request.setRequestHeader("Host", "wsmvip.aisidi.com");
                        request.setRequestHeader("Content - Length", "76");
                    },
                    success: function (data) {
                        if (data.Code == "0000") {
                            alert(JSON.stringify(data.Data));
                            alert("Token:" + data.Data.Token);
                        } else {
                            alert('登录失败:' + data.Message);
                        }
                    },
                    complete: function (XMLHttpRequest, textStatus) {
                        this; // 调用本次AJAX请求时传递的options参数
                    }
                });
            }
        </script>
    复制代码

    后台部分:

    复制代码
        /// <summary>
        /// 后台再请求别的接口的方法,体现了添加request header
        /// </summary>
        /// <returns></returns>
        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;
        }
    复制代码

    如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏我一杯咖啡【物质支持】,也可以点击文章下方“推荐”按钮【精神支持】,您的“推荐”将是我最大的写作动力!
    欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,凡是转载于本人的文章,不能设置打赏功能,如有特殊需求请与本人联系!
  • 相关阅读:
    LeetCode#13罗马数字转整数
    LeetCode#7整数反转
    LeetCode#1两数之和
    LeetCode#26删除排序数组中的重复项
    LeecCode#1550存在连续三个奇数的数组
    LeetCode#228汇总区间
    LeetCode#1476子矩形查询
    LeetCode#1535找出数组游戏的赢家
    LeetCode#867转置矩阵
    Vue源码——摸着石头过河
  • 原文地址:https://www.cnblogs.com/Alex80/p/12020840.html
Copyright © 2020-2023  润新知