Created
August 5, 2015 06:23
-
-
Save sappho192/e1fdffc1f099c0efc18b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* HTTP 요청 함수들 */ | |
| public string RequestWebPage(string url, string sendData, CookieContainer cook, Encoding encoding, string referer = "", string origin = "", string host = "") | |
| { | |
| var req = (HttpWebRequest)WebRequest.Create(url); | |
| if (!host.Equals("")) { req.Host = host; } | |
| req.Headers.Add(@"Cache-Control", @"max-age=0"); | |
| if (!origin.Equals("")) { req.Headers.Add(@"Origin", origin); } | |
| req.Headers.Add(@"Accept-Encoding", @"gzip,deflate"); | |
| req.UserAgent = @"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"; | |
| req.Accept = @"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; | |
| req.Method = @"POST"; | |
| req.ContentLength = sendData.Length; | |
| req.ContentType = @"application/x-www-form-urlencoded"; | |
| req.KeepAlive = true; | |
| req.CookieContainer = cook; // 세션 저장 쿠키 | |
| req.ServicePoint.UseNagleAlgorithm = false; | |
| req.ServicePoint.Expect100Continue = false; | |
| // 속도 개선 | |
| if (!isProxyEnabled()) { req.Proxy = WebRequest.DefaultWebProxy; } | |
| if (!referer.Equals("")) { req.Referer = referer; } | |
| // req.Credentials = CredentialCache.DefaultCredentials; | |
| try | |
| { | |
| StreamWriter writer = new StreamWriter(req.GetRequestStream()); | |
| writer.Write(sendData); | |
| writer.Close(); | |
| using (HttpWebResponse result = (HttpWebResponse)req.GetResponse()) | |
| { | |
| result.Cookies = req.CookieContainer.GetCookies(req.RequestUri); | |
| // 요청이 성공적일 때 | |
| if (result.StatusCode.Equals(HttpStatusCode.OK)) | |
| { | |
| cookies.Add(result.Cookies); | |
| //Encoding encode = Encoding.GetEncoding("UTF-8"); | |
| using (Stream strReceiveStream = result.GetResponseStream()) | |
| { | |
| if (strReceiveStream != null) | |
| { | |
| StreamReader reqStreamReader = new StreamReader(strReceiveStream, encoding); | |
| string strResult = reqStreamReader.ReadToEnd(); // 데이터 읽어옴 | |
| req.Abort(); | |
| strReceiveStream.Close(); | |
| reqStreamReader.Close(); | |
| return strResult; | |
| } | |
| } | |
| } | |
| else | |
| { | |
| return "ERR"; | |
| } | |
| } | |
| return "ERR"; | |
| } | |
| catch (WebException e) | |
| { | |
| if (e.Status.Equals(WebExceptionStatus.NameResolutionFailure)) | |
| { | |
| //MessageBox.Show("인터넷 연결을 확인해주세요"); | |
| return "ERR"; | |
| } | |
| if (e.Status.Equals(WebExceptionStatus.ProtocolError)) | |
| { | |
| return "ERR"; | |
| } | |
| throw; | |
| } | |
| } | |
| public string MoveToAnotherPage(string url, CookieContainer cook, Encoding encoding, string referer = "") | |
| { | |
| if (cook == null) throw new ArgumentNullException("cook"); | |
| var req = (HttpWebRequest)WebRequest.Create(url); | |
| req.CookieContainer = cook; // 보관된 cookie 이용 | |
| req.Referer = referer; | |
| req.ServicePoint.Expect100Continue = false; | |
| req.ServicePoint.UseNagleAlgorithm = false; | |
| if (!isProxyEnabled()) { req.Proxy = null; } | |
| //req.Credentials = CredentialCache.DefaultCredentials; | |
| try | |
| { | |
| using (HttpWebResponse result = (HttpWebResponse)req.GetResponse()) | |
| { | |
| result.Cookies = req.CookieContainer.GetCookies(req.RequestUri); | |
| cookies.Add(result.Cookies); // 추가적인 cookie 보관 | |
| using (Stream strm = result.GetResponseStream()) | |
| { | |
| if (strm != null) | |
| { | |
| StreamReader sr = new StreamReader(strm, encoding, true); | |
| string responseHtml = sr.ReadToEnd(); | |
| sr.Close(); | |
| strm.Close(); | |
| return responseHtml; | |
| } | |
| } | |
| } | |
| } | |
| catch (WebException e) | |
| { | |
| if (e.Status.Equals(WebExceptionStatus.NameResolutionFailure)) | |
| { | |
| //MessageBox.Show("인터넷 연결을 확인해주세요"); | |
| return "ERR"; | |
| } | |
| if (e.Status.Equals(WebExceptionStatus.ProtocolError)) | |
| { | |
| return "ERR"; | |
| } | |
| throw; | |
| } | |
| return "ERR"; | |
| } | |
| private bool isProxyEnabled() | |
| { | |
| if (WebRequest.DefaultWebProxy.Credentials == null) | |
| { | |
| return false; | |
| } | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment