예제 #1
0
파일: HttpPost.cs 프로젝트: T2BE/Common
 protected void OnError(NetException exception)
 {
     if (null != Error)
         Error(this, new EventArgs<NetException>(exception));
 }
예제 #2
0
파일: HttpPost.cs 프로젝트: T2BE/Common
        public byte[] Post()
        {
            byte[] result = null;
            try
            {

                string requestContentType = String.Empty;
                switch (PostEncodingType)
                {
                    case PostEncodingTypes.UrlEncoded:
                        requestContentType = "application/x-www-form-urlencoded";
                        break;
                    default:
                        requestContentType = "application/x-www-form-urlencoded";
                        break;
                }
                string encodedParameters = EncodeParameters();
                HttpWebRequest request = null;


                request = (HttpWebRequest)WebRequest.Create(PostUri);
                if (NetworkCredential != null)
                    request.Credentials = NetworkCredential;
                if (PostType == PostTypes.Post)
                {                    
                    request.Method = "POST";
                    request.ContentType = requestContentType;
                    request.ContentLength = encodedParameters.Length;
                    using (Stream writeStream = request.GetRequestStream())
                    {
                        UTF8Encoding encoding = new UTF8Encoding();
                        byte[] bytes = encoding.GetBytes(PostData);
                        writeStream.Write(bytes, 0, bytes.Length);
                    }
                }
                else
                {
                    var uri = PostUri.ToString() + encodedParameters;
                    request = (HttpWebRequest)WebRequest.Create(uri);
                    if (NetworkCredential != null)
                        request.Credentials = NetworkCredential;
                    request.Method = "GET";
                }            
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        OnRecieved(responseStream);
                        using (StreamReader readStream = new StreamReader(responseStream, Encoding))
                        {
                            result = Encoding.GetBytes(readStream.ReadToEnd());
                        }
                        
                    }
                }              
                
            }
            catch (System.Net.WebException wex)
            {
                var netException = new NetException("HttpPost Failed", wex);
                OnError(netException);
            }
            catch (System.Exception ex)
            {
                var netException = new NetException("HttpPost Failed", ex);
                OnError(netException);
            }
            return result;
        }