C# http form-data 参数


using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace ConsoleApp1
{
    public  class HttpFormData
    {

        /// <summary>
        /// 测试http form-data 类型参数
        /// </summary>
        /// <returns></returns>
        public static string GetCookie()
        {
            string Cookie = string.Empty;
            try
            {
                var httpConfig = "https://XXXX";
                HttpWebRequest request = WebRequest.Create(httpConfig) as HttpWebRequest;
                //创建http请求实例
                request.Method = "post";
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                StringBuilder buffer = new StringBuilder();
                //request.Headers.Add("Cookie", cookie);
                buffer.AppendFormat("{0}={1}&", "phonenumber", "XXXX");
                buffer.AppendFormat("{0}={1}", "password", "XXX");
                byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
                Stream stream = request.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Cookie = response.Headers["Set-Cookie"];
            }
            catch (Exception ex)
            {
                Console.WriteLine("登录异常:" + ex.Message);
                return "";
            }

            return Cookie;
        }
    }
}

 

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM