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)
                    {
    
                    }
    
                }
            }
        }
  • 相关阅读:
    在线免费学习全世界的课程
    【安卓】imageView.scaleType取centerCrop后,再用padding时显示异常?
    win8+VS2012搭建OpenGL超级宝典的环境
    leetcode第一刷_Plus One
    HASH JION AND NESTED JION
    Hive创建外部表以及分区
    Android数字签名解析(一)
    Alex 的 Hadoop 菜鸟教程: 第3课 Hadoop 安装教程
    用C#生成不反复的随机数
    Swift数据类型及数据类型转换
  • 原文地址:https://www.cnblogs.com/weixin18/p/15752079.html
Copyright © 2011-2022 走看看