一.提交方法

  /// <summary>
        /// MultipartFormData Post方式提交
        /// </summary>
        /// <param name="url"></param>
        /// <param name="kVDatas"></param>
        /// <param name="method"></param>
        /// <param name="encoding"></param>
        /// <param name="timeOut"></param>
        /// <returns></returns>
        public  string HttpPostMultipartFormData(string url, NameValueCollection kVDatas, string method = WebRequestMethods.Http.Post, string encoding = "UTF-8", int timeOut = -1)
        {
            string resultStr = "";
            try
            {
                string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
                byte[] boundarybytes = Encoding.GetEncoding(encoding).GetBytes("\r\n--" + boundary + "\r\n");
                byte[] endbytes = Encoding.GetEncoding(encoding).GetBytes("\r\n--" + boundary + "--\r\n");

                ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //一定要有这一句
                ServicePointManager.ServerCertificateValidationCallback = delegate(
                Object obj, X509Certificate certificate, X509Chain chain,
                SslPolicyErrors errors)
                {
                    return (true);
                };



                //1.HttpWebRequest
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ContentType = "multipart/form-data; boundary=" + boundary;
                request.Method = method;
                request.KeepAlive = true;
                request.Timeout = timeOut;


                using (Stream stream = request.GetRequestStream())
                {
                    //1.1 key/value
                    string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
                    if (kVDatas != null)
                    {
                        foreach (string key in kVDatas.Keys)
                        {
                            stream.Write(boundarybytes, 0, boundarybytes.Length);
                            string formitem = string.Format(formdataTemplate, key, kVDatas[key]);
                            byte[] formitembytes = Encoding.GetEncoding(encoding).GetBytes(formitem);
                            stream.Write(formitembytes, 0, formitembytes.Length);
                        }
                    }
                    stream.Write(endbytes, 0, endbytes.Length);
                }
                //2.WebResponse
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader stream = new StreamReader(response.GetResponseStream()))
                    {
                        resultStr = stream.ReadToEnd();

                    }
                }


            }
            catch (WebException ex)
            {
                var errorSr = new StreamReader(ex.Response.GetResponseStream());
                resultStr = errorSr.ReadToEnd();
            }
            return resultStr;
        }

二.NameValueCollection 

NameValueCollection keyData = new NameValueCollection();
keyData.Add("A","222"); 
keyData.Add("B","3333");
keyData.Add("key_words", "4444");

三.NameValueCollection 转换为json格式(先把NameValueCollection 转换为Dictionary,再把Dictionary转换为json)

     /// <summary>
      /// NameValueCollection 转换为json格式
      /// </summary>
      /// <param name="source"></param>
      /// <returns></returns>
      public  IDictionary<string, string> NameVlueToDictionary(NameValueCollection source)
      {
          return source.AllKeys.ToDictionary(k => k, k => source[k]);
      }

var keyDict =NameVlueToDictionary(keyData);

string strJSON = JsonConvert.SerializeObject(keyDict);

 

四.string转换为NameValueCollection 

        NameValueCollection kVDatas = HttpUtility.ParseQueryString(strUpData);

posted on 2021-10-18 15:10  梦想&现实  阅读(2850)  评论(0编辑  收藏  举报