Http 통신을 수행하는 Client Class
예제 #1
0
        /// <summary>
        /// 지정된 Web 주소로부터 Http Get을 이용하여 정보를 다운 받는다.
        /// </summary>
        /// <param name="url">서버 주소</param>
        /// <param name="timeout">제한 시간</param>
        /// <param name="userId">인증을 위한 사용자 Id</param>
        /// <param name="password">인증을 위한 비밀번호</param>
        /// <returns>응답 개체 (<c>WebResponse</c> 개체)</returns>
        public static WebResponse GetResponse(string url, int timeout = HttpConsts.HTTP_DEFAULT_TIMEOUT, string userId = null,
                                              string password = null) {
            if(IsDebugEnabled)
                log.Debug("Get HttpResponse... url=[{0}], timeout=[{1}], userId=[{2}], password=[{3}]", url, timeout, userId, password);

            var httpClient = new HttpClient(url, timeout, userId, password);
            return httpClient.GetResponse(WebRequestMethods.Http.Get);
        }
예제 #2
0
        public void HttpGet2() {
            var http = new HttpClient(Echo2 + "?" + HtmlTool.UrlEncode(PayLoad));
            var result = http.Get();
            Assert.IsNotEmpty(result);
            if(IsDebugEnabled)
                log.Debug(result.EllipsisChar(80));

            http = new HttpClient(Echo1 + "?" + HtmlTool.UrlEncode(PayLoad, Encoding.UTF8));

            result = http.Get();
            Assert.IsNotEmpty(result, "Script=" + http.BaseUriString);
            if(IsDebugEnabled)
                log.Debug(result.EllipsisChar(80));
        }
예제 #3
0
        /// <summary>
        /// 주어진 URL에 HTTP GET을 수행한다.
        /// </summary>
        /// <param name="url">Server URL</param>
        /// <param name="enc">Encoding Type</param>
        /// <param name="timeout">Connection Timeout (milliseconds)</param>
        /// <param name="userId">UserId for Authentication</param>
        /// <param name="password">Password for Authentication</param>
        /// <returns></returns>
        public static string GetString(string url, Encoding enc = null, int timeout = HttpConsts.HTTP_DEFAULT_TIMEOUT,
                                       string userId = null, string password = null) {
            url.ShouldNotBeWhiteSpace("url");

            enc = enc ?? StringTool.DefaultEncoding;

            if(IsDebugEnabled)
                log.Debug("Http GetString... url=[{0}], enc=[{1}], timeout=[{2}], userId=[{3}], password=[{4}]", url, enc, timeout,
                          userId, password);

            var httpClient = new HttpClient(url, timeout, userId, password);
            var result = httpClient.Get();

            if(IsDebugEnabled)
                log.Debug("HTTP GET 방식의 반환 문자열=[{0}]", result.EllipsisChar(255));

            return result;
        }
예제 #4
0
        /// <summary>
        /// 지정된 Web 주소로부터 Http Post을 이용하여 정보를 전달하고, 결과 정보를 얻는다.
        /// </summary>
        /// <param name="url">서버 주소</param>
        /// <param name="payload">POST 될 데이타 (형식 : PARAM1=VALUE1&amp;PARAM2=VALUE2&amp;PARAM3=VALUE3)</param>
        /// <param name="enc">인코딩 방식</param>
        /// <param name="timeout">제한 시간</param>
        /// <param name="userId">인증을 위한 사용자 Id</param>
        /// <param name="password">인증을 위한 비밀번호</param>
        /// <returns>응답 개체 (<c>WebResponse</c> 개체)</returns>
        public static string PostString(string url,
                                        string payload,
                                        Encoding enc = null,
                                        int timeout = HttpConsts.HTTP_DEFAULT_TIMEOUT,
                                        string userId = null,
                                        string password = null) {
            url.ShouldNotBeWhiteSpace("url");
            enc = enc ?? StringTool.DefaultEncoding;

            if(IsDebugEnabled)
                log.Debug("Post String... url=[{0}], timeout=[{1}], userId=[{2}], password=[{3}]", url, timeout, userId, password);

            var result = new HttpClient(url, timeout, userId, password).Post(payload, enc);

            if(IsDebugEnabled)
                log.Debug("Post result string=[{0}]", result.EllipsisChar(255));

            return result;
        }
예제 #5
0
        public void HttpGet() {
            foreach(string script in ScriptPaths) {
                var http = new HttpClient(script + "?" + HtmlTool.UrlEncode(PayLoad));

                var result = http.Get();
                Assert.IsNotEmpty(result, "Result is Empty. script=" + script);

                if(IsDebugEnabled)
                    log.Debug(result.EllipsisChar(80));
            }

            if(IsDebugEnabled)
                log.Debug("============================");

            foreach(string script in ScriptPaths) {
                var httpUtf8 = new HttpClient(script + "?" + HtmlTool.UrlEncode(PayLoad, Encoding.UTF8));

                var result = httpUtf8.Get();
                Assert.IsNotEmpty(result, "Result is Empty. script=" + script);

                if(IsDebugEnabled)
                    log.Debug(result.EllipsisChar(80));
            }
        }
예제 #6
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("");
            }
        }
예제 #7
0
        public void Compression() {
            TestTool.RunTasks(5,
                              () => {
                                  // 압축전송을 제공하는 Web Site의 경우 Stream을 자동 압축/해제를 해준다. (.NET 2.0 이상부터)
                                  //
                                  var http = new HttpClient("http://intra.realweb21.com/Intra/Home/tabid/36/language/ko-KR/Default.aspx");
                                  var result = http.Get();

                                  Assert.IsNotEmpty(result);

                                  if(IsDebugEnabled)
                                      log.Debug("Compression=" + result.EllipsisChar(1024));
                              });
        }
예제 #8
0
        public void DownloadFile() {
            var http = new HttpClient();

            http.DownloadFile(FileUrl, @"C:\Temp\File.doc");
        }
예제 #9
0
        public void DownloadData() {
            var http = new HttpClient(Echo1 + "?" + HtmlTool.UrlEncode(PayLoad, Encoding.UTF8));

            var data = http.DownloadData();
            Assert.IsNotNull(data);
            Assert.Greater(data.Length, 0);

            if(IsDebugEnabled)
                log.Debug("Response = " + Encoding.UTF8.GetString(data).EllipsisChar(80));
        }
예제 #10
0
        public void HttpPost2() {
            var list = new NameValueCollection
                       {
                           { "postparam", "리얼웹 가나다 ABC 123 & _ -" }
                       };

            var http = new HttpClient(Echo2);
            var result = http.Post(list, Encoding.Default);

            Assert.IsNotEmpty(result);
            if(IsDebugEnabled)
                log.Debug(result.EllipsisChar(80));
        }
예제 #11
0
        public void HttpGetToMailTest() {
            using(var message = new MailMessage()) {
                message.From = new MailAddress(FromAddress);

                var toAddrs = ToAddress.Split(';', ',');
                foreach(string toAddr in toAddrs)
                    message.To.Add(toAddr);

                // 제목
                //
                message.Subject = "HttpGet을 이용한 본문 메시지입니다." + GetTesterInformation();
                message.SubjectEncoding = Encoding.UTF8;

                // 본문
                //
                message.IsBodyHtml = true; // Html 인지 설정

                // Html Encoding관련 문제가 있다.
                //
                // HttpClient http = new HttpClient("http://www.codeproject.com/");
                var http = new HttpClient("http://www.naver.com/");
                message.Body = http.Get();
                // message.BodyEncoding = Encoding.GetEncoding("euc-kr");
                message.IsBodyHtml = true;

                // 첨부파일
                message.Attachments.Add(new Attachment(AttachFileName));
                message.Attachments.Add(new Attachment(AttachFileName2));

                // 로그인 정보를 입력하고, 메일을 보낸다.
                //
                var mailClient = new SmtpClient(SmtpServer);
                mailClient.Credentials = new NetworkCredential(UserName, Password);
                mailClient.Send(message);
            }
        }
예제 #12
0
        /// <summary>
        /// 가장 일반적으로 사용할 함수, 서버에 HTTP로 접속하여 Response를 얻는다.
        /// </summary>
        /// <param name="method">전송 방법 (<see cref="WebRequestMethods.Http"/>참조)</param>
        /// <param name="url">서버 주소</param>
        /// <param name="timeout">제한 시간</param>
        /// <param name="userId">UserId</param>
        /// <param name="password">Password</param>
        /// <returns>결과 <c>WebResponse</c> 인스턴스 개체</returns>
        /// <remarks>WebResponse는 사용한 후 꼭 Close를 호출해야 한다.</remarks>
        public static WebResponse GetResponse(string method,
                                              string url,
                                              int timeout = HttpConsts.HTTP_DEFAULT_TIMEOUT,
                                              string userId = null,
                                              string password = null) {
            method.ShouldNotBeWhiteSpace("method");
            url.ShouldNotBeWhiteSpace("url");

            if(IsDebugEnabled)
                log.Debug("Get HttpResponse... method=[{0}], url=[{1}], timeout=[{2}], userId=[{3}], password=[{4}]", method, url,
                          timeout, userId, password);

            var httpClient = new HttpClient(url, timeout, userId, password);
            return httpClient.GetResponse(method);
        }