Http Post请求之Basic验证

     /// 
        /// HttpPost Basic 认证
        /// 
        /// 远程地址超链
        /// 数据
        /// Basic 账号
        /// Basic 密码
        /// 
        public static string HttpPostBasicJson(string Url, object postDataStr, string userName, string pwd)
        {
            string strRespone = "";
            string json = JsonHelper.ConvertToJson(postDataStr);
            ServicePointManager.DefaultConnectionLimit = 300;
            System.GC.Collect();
            CookieContainer cookieContainer = new CookieContainer();
            // 设置提交的相关参数 
            HttpWebRequest request = null;
            HttpWebResponse SendSMSResponse = null;
            Stream dataStream = null;
            StreamReader SendSMSResponseStream = null;
            try
            {
                request = WebRequest.Create(Url) as HttpWebRequest;
                request.Method = "POST";
                request.KeepAlive = false;
                request.ServicePoint.ConnectionLimit = 300;
                request.AllowAutoRedirect = true;
                request.Timeout = 100000;
                request.ReadWriteTimeout = 100000;

                // 生成Basic 鉴权编码
                string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", userName, pwd)));

                //添加Authorization到HTTP头
                request.Headers.Add("Authorization", "Basic " + code);
                request.ContentType = "application/json";  
                byte[] bytes = Encoding.UTF8.GetBytes(json);
                request.Proxy = null;
                request.CookieContainer = cookieContainer;
                using (dataStream = request.GetRequestStream())
                {
                    dataStream.Write(bytes, 0, bytes.Length);
                }
                SendSMSResponse = (HttpWebResponse)request.GetResponse();
                if (SendSMSResponse.StatusCode == HttpStatusCode.RequestTimeout)
                {
                    if (SendSMSResponse != null)
                    {
                        SendSMSResponse.Close();
                        SendSMSResponse = null;
                    }
                    if (request != null)
                    {
                        request.Abort();
                    }
                    return null;
                }
                SendSMSResponseStream = new StreamReader(SendSMSResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                strRespone = SendSMSResponseStream.ReadToEnd();

            }
            catch (System.Exception ex)
            {
                if (dataStream != null)
                {
                    dataStream.Close();
                    dataStream.Dispose();
                    dataStream = null;
                }
                if (SendSMSResponseStream != null)
                {
                    SendSMSResponseStream.Close();
                    SendSMSResponseStream.Dispose();
                    SendSMSResponseStream = null;
                }
                if (SendSMSResponse != null)
                {
                    SendSMSResponse.Close();
                    SendSMSResponse = null;
                }
                if (request != null)
                {
                    request.Abort();
                }
                throw ex;
            }
            finally
            {
                if (dataStream != null)
                {
                    dataStream.Close();
                    dataStream.Dispose();
                    dataStream = null;
                }
                if (SendSMSResponseStream != null)
                {
                    SendSMSResponseStream.Close();
                    SendSMSResponseStream.Dispose();
                    SendSMSResponseStream = null;
                }
                if (SendSMSResponse != null)
                {
                    SendSMSResponse.Close();
                    SendSMSResponse = null;
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            return strRespone;
        }

关键代码:

             

  // 生成Basic 鉴权编码
  string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", userName, pwd)));       
 //添加Authorization到HTTP头
   request.Headers.Add("Authorization", "Basic " + code);

 

你可能感兴趣的:(C#,.net,WebApi)