티스토리 뷰

반응형

C#에서 사용 할수 있는 Http 또는 Https Url Connection Class Lib

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using CHttpLib;

namespace CHttpLib
{  
    abstract public class CHttpClient
    {
        private String url;
        
        private String resultCode;
        private String resultMsg;

        private List parameterName;
        private List parameterValue;

        public void SetURL(String s) { url = s; }
        public String GetURL() { return url; }
        public abstract void ResponseCallBack(String s);

        public void setResultCode(String s) { resultCode = s; }
        public String getResultCode() { return resultCode; }
        public void setResultMsg(String s) { resultMsg = s; }
        public String getResultMsg() { return resultMsg; }
        
        CookieCollection cookiecollection;

        public CHttpClient()
        {
            Init();
        }

        public CookieCollection getCooki()
        {
            return (cookiecollection);
        }

        public void setCooki(CookieCollection cc)
        {
            cookiecollection = cc;
        }
                
        public void Init()
        {
            parameterName = new List();
            parameterValue = new List();
        }

        public void AddParameter(String sName, String sValue)
        {
            parameterName.Add(sName);
            parameterValue.Add(sValue);
        }

        // X.509 SSL Define (private OCP)
        public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {            
            return true;
        }

        public void connectCallBack()
        {            
            ResponseCallBack(connect());
        }

        public String connect() 
        {
            byte[] parameters = GetParameterList();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Proxy = null;
            request.Credentials = CredentialCache.DefaultCredentials;
            ServicePointManager.ServerCertificateValidationCallback +=
                new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            request.CookieContainer = new CookieContainer();
            if (cookiecollection != null) request.CookieContainer.Add(cookiecollection);

            request.ContentLength = parameters.Length;

            try
            {
                System.IO.Stream postDataStream = request.GetRequestStream();
                postDataStream.Write(parameters, 0, parameters.Length);
                postDataStream.Close();
            }
            catch (Exception e)
            {
                throw e;
            }

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    if (cookiecollection == null)  cookiecollection = response.Cookies;

                    System.IO.Stream respPostStream = response.GetResponseStream();                       
                    System.IO.StreamReader readerPost = new System.IO.StreamReader(respPostStream, Encoding.UTF8);
                    Encoding en = readerPost.CurrentEncoding;

                    return (readerPost.ReadToEnd());
                }
                else
                {
                    throw new Exception("서버로 부터 비 정상 메세지 수신 함 Error Code [" + response.StatusCode + "]");
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private byte[] GetParameterList()
        {
            int i = 0;            
            StringBuilder PostData = new StringBuilder();
            Encoding encoding = Encoding.UTF8;
                      
            for (i = 0; i < parameterName.Count; i++)
            {
                PostData.Append(parameterName[i]);
                PostData.Append("=");
                PostData.Append(parameterValue[i]);
                PostData.Append("&");
            }                  
           return (encoding.GetBytes(PostData.ToString()));            
        }
    }
}
반응형
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함