Http Post 요청 Basic 검증

4639 단어 C#.netWebApi
     /// 
        /// 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);

좋은 웹페이지 즐겨찾기