• HTTP & HTTPs


    HTTP

    HTTP 消息

    HTTP 方法

    基础知识参见:HTTP 协议初识 - 阮一峰

    http请求

    在 http请求 - Node直出理论与实践总结 中,从正常模式到数据直出再到服务端渲染直出,对http请求不断优化,加快首屏渲染时间。

    HTTPS

    基础知识参见: HTTPS 升级指南 - 阮一峰

    问题整理

    使用场景:字典集参数拼接成querystring

    public static string DicToQueryString(Dictionary<string, object> dic, bool isSort, bool isUrlEncode)
    {
        Dictionary<string, object> tempDic = (isSort) ? 
            dic.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value) : dic;
    
        StringBuilder sb = new StringBuilder();
        foreach (KeyValuePair<string, object> k in tempDic) {
            string key = k.Key, val = "";
            object value = k.Value;
            if (value == null || (value is string && string.IsNullOrWhiteSpace(value.ToString()))) { continue; }
    
            if (value is string) { val = value.ToString(); }
            else { val = JsonConvert.SerializeObject(value); }
            val = (isUrlEncode) ? StringUtils.UrlEncode(val) : val;
    
            sb.Append(key).Append("=").Append(val).Append("&");
        }
    
        string querystring = sb.ToString();
        if (querystring.Length > 1) { querystring = querystring.Substring(0, querystring.Length - 1); }
        return querystring;
    }
    

    其中,参数isSort表示key是否排序,isUrlEncode表示value是否urlEncode处理。value的编解码方法如下

    public static string UrlEncode(string str)
    {
        try {
            string[] temp = str.Split(' ');
            StringBuilder sb = new StringBuilder();
            for (int t = 0; t < temp.Length; t++) {
                sb.Append(HttpUtility.UrlEncode(temp[t].ToString(), Encoding.UTF8));
                if (t < temp.Length - 1) { sb.Append("%20"); }
            }
            return sb.ToString();
        } catch (Exception e) { return null; }
    }
    public static string UrlDecode(string str)
    {
        try {
            return HttpUtility.UrlDecode(str, Encoding.UTF8);
        } catch (Exception e) { return null; }
    }
    

    此处提供一个扩展工具类,推荐

    /// <summary>
    /// HttpUtility扩展
    /// HttpUtility的urlencode会将空格编码成+,urldecode会将+解码成空格
    /// </summary>
    public class HttpUtilityEx
    {
    	private static string bankEncode = HttpUtility.UrlEncode(" ");
    	private static string plusEncode = HttpUtility.UrlEncode("+");
    	
    	/// 空格符的编码(+)转换为正常编码方式(%20),默认编码为UTF8
    	public static string UrlEncode(string url) {
    		return UrlEncode(url,Encoding.UTF8);
    	}
    
    	/// 空格符的编码("+")转换为正常编码方式(%20)
    	public static string UrlEncode(string url, Encoding e) {
    		if (string.IsNullOrEmpty(url)) { return string.Empty; }
    		
    		string tmp = HttpUtility.UrlEncode(url,e);		
    		tmp = tmp.Replace(bankEncode, "%20");//将HttpUtility.UrlEncode编码后的空格符(+)替换成%20
    		return tmp;
    	}
    
    	/// 保证+不会被decode成空格。默认编码UTF8
    	public static string UrlDecode(string url) {
    		return UrlDecode(url,Encoding.UTF8);
    	}
    
    	/// 保证+不会被decode成空格
    	public static string UrlDecode(string url,Encoding e) {
    		if (string.IsNullOrEmpty(url)) { return string.Empty; }
    		
    		//参数中有+,先替换成HttpUtility.UrlEncode后的编码,以便HttpUtility.UrlDecode时能够还原成+,而不是把+解密成空格
    		url = url.Replace("+",plusEncode);
    		return HttpUtility.UrlDecode(url,e);
    	}
    }
    

    请求WebApi报错:400 Bad Request

    解决参考:https://blog.csdn.net/lw1242189467/article/details/80048407

  • 相关阅读:
    [转]CSS实现三角形的方法
    border:none 与border:0的区别
    jQuery选择器总结
    [转]利用CSS、JavaScript及Ajax实现图片预加载的三大方法
    [转]Javascript实现图片的预加载的完整实现
    [转]Javascript实现图片的预加载
    [转]资源预加载
    [转]css,javascript的预加载
    [转]预加载资源研究
    [转]移动WEB开发常用技巧
  • 原文地址:https://www.cnblogs.com/wjcx-sqh/p/5929906.html
Copyright © 2020-2023  润新知