• 使用https post 调用接口(不带证书)


    /// <summary>
            /// 使用https post 调用接口
            /// </summary>
            /// <param name="url">接口地址</param>
            /// <param name="pm_str">参数</param>
            /// <param name="pm_ContentType">application/x-www-form-urlencoded</param>
            /// <param name="myDictionary">hender没有就给NUll</param>
            /// <returns></returns>
            public static string PostUrl(string url, string pm_str, string pm_ContentType, Dictionary<string, string> myDictionary)
            {
                //string headstr = null;
                StreamReader reader = null;
                Stream requestStream = null;
                HttpWebResponse Response = null;
                try
                {
                    //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                    HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
                    myReq.Method = "POST";
                    if (pm_ContentType != null)
                    {
                        myReq.ContentType = pm_ContentType;
                    }
                    if (myDictionary != null)
                    {
                        foreach (KeyValuePair<string, string> kvp in myDictionary)
                        {
                            myReq.Headers.Add(kvp.Key, kvp.Value);
                        }
    
                        // myReq.Headers.Add("Authorization", "Basic " + headstr);
                    }
                    byte[] bz = System.Text.Encoding.UTF8.GetBytes(pm_str);
                    myReq.ContentLength = bz.Length;
                    requestStream = myReq.GetRequestStream();
                    requestStream.Write(bz, 0, bz.Length);
                    requestStream.Close();
                    requestStream = null;
                    //myReq.Timeout = 30000;
    
                    Response = (HttpWebResponse)myReq.GetResponse();
                    reader = new StreamReader(Response.GetResponseStream(), Encoding.UTF8);
                    string retstr = reader.ReadToEnd().Trim();
                    return retstr;
                }
                catch (Exception ex)
                {
                    return "{\"code\":\"E\",\"msg\":\"ERR" + ex.Message.ToString() + "\"}";
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                        reader = null;
                    }
                    if (requestStream != null)
                    {
    
                    }
    
                }
            }
        }
  • 相关阅读:
    给VPS装桌面
    GIT免密码PUSH
    验证码类
    Url几个常用的函数
    PHP--关于模板的原理和解析
    管理员权限执行批处理和静默执行regsvr32注册
    linux 维护常见场景小命令 (未完待续)
    批处理定时重启print打印服务,解决打印机异常队列堆积
    Linux----LVM扩容磁盘空间
    6、Samba 服务器配置
  • 原文地址:https://www.cnblogs.com/weixin18/p/15752079.html
Copyright © 2020-2023  润新知