zoukankan      html  css  js  c++  java
  • 使用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)
                    {
    
                    }
    
                }
            }
        }
  • 相关阅读:
    Ubuntu 只能用guest登录的问题修复
    Ubuntu下编译C语言程序(同时给编译生成的文件命名)
    设置PYTHONIOENCODING
    Ubuntu安装atom
    R语言中将数据框(data.frame)中字符型数据转化为数值型
    修改记事本默认编码为UTF-8
    Java虚拟机浅探
    制作宅基腐主页 && 制作个人简历--材料:BootStrap
    尝试用有限状态机解决一道算法题
    OOP,WEB开发实用小技巧
  • 原文地址:https://www.cnblogs.com/weixin18/p/15752079.html
Copyright © 2011-2022 走看看