NSoft.NFramework.Networks.HttpClient.Post C# (CSharp) Method

Post() public method

주어진 URL를 기초로 Http Post 를 수행한다. ASP.NET 서버인 경우 web.config의 Request/Response Encoding 정보를 잘 보고 해야 한다.
public Post ( string payload, Encoding enc = null ) : string
payload string 전송할 DATA (ex PARAM1=VALUE1&PARAM2=VALUE2)
enc System.Text.Encoding 전송할 DATA의 Encoding 방식
return string
        public string Post(string payload, Encoding enc = null) {
            enc = enc ?? StringTool.DefaultEncoding;

            if(IsDebugEnabled)
                log.Debug("Execute Http POST method. payload=[{0}], encoding=[{1}]", payload, enc.BodyName);

            string result;

            try {
                Request.Method = WebRequestMethods.Http.Post.ToUpper();

                // request 로 보낼 때 문자 인코딩 정보도 같이 보내야 한다.
                Request.ContentType = HttpConsts.HTTP_POST_CONTENT_TYPE + "; charset=" + enc.WebName;
                Request.ContentLength = 0;

                if(payload.IsNotEmpty()) {
                    byte[] data = enc.GetBytes(payload);
                    Request.ContentLength = data.Length;


                    var requestStream = Request.GetRequestStream();
                    requestStream.Write(data, 0, data.Length);
                    requestStream.Close();
                }

                using(var response = (HttpWebResponse)Request.GetResponse())
                using(var responseStream = response.GetResponseStream()) {
                    result = responseStream.ToText();
                }
            }
            finally {
                _request = null;
            }

            if(IsDebugEnabled)
                log.Debug("Http POST result=[{0}]", result.EllipsisChar(80));

            return result;
        }

Same methods

HttpClient::Post ( NameValueCollection inputs, Encoding enc = null ) : string

Usage Example

Example #1
0
        public void HttpPost() {
            foreach(string script in ScriptPaths) {
                var http = new HttpClient(script);

                log.Debug("Post(string) = " + http.Post(PayLoad, Encoding.UTF8));
                log.Debug("Post(string) = " + http.Post(PayLoad, Encoding.Default));

                var nvc = new NameValueCollection
                          {
                              { "A", "123" },
                              { "B", "가나다" },
                              { "CD", "각하" },
                              { "Name", "바보 아냐" }
                          };

                log.Debug("Post(NameValueCollection inputs) = " + http.Post(nvc, Encoding.UTF8));
                log.Debug("Post(NameValueCollection inputs) = " + http.Post(nvc, Encoding.Default));
                log.Debug("");
            }
        }
All Usage Examples Of NSoft.NFramework.Networks.HttpClient::Post