댓글 쓰기 권한이 없습니다. 로그인 하시겠습니까?
C#
2014.02.18 23:58
HttpWebRequest 로 http GET, POST 전송 및 처리
조회 수 30802 댓글 0
C# 에서 GET 혹은 POST 방식으로 http 리퀘스트를 보내는 예제입니다.
HttpWebRequest wReq; HttpWebResponse wRes; private bool getResponse(String url, string cookie, string data) { string cookie; try { uri = new Uri(url); // string 을 Uri 로 형변환 wReq = (HttpWebRequest)WebRequest.Create(uri); // WebRequest 객체 형성 및 HttpWebRequest 로 형변환 wReq.Method = "GET"; // 전송 방법 "GET" or "POST" wReq.ServicePoint.Expect100Continue = false; wReq.CookieContainer = new CookieContainer(); wReq.CookieContainer.SetCookies(uri, cookie); // 넘겨줄 쿠키가 있을때 CookiContainer 에 저장 /* POST 전송일 경우 byte[] byteArray = Encoding.UTF8.GetBytes(data); Stream dataStream = wReq.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); */ using (wRes = (HttpWebResponse)wReq.GetResponse()) { Stream respPostStream = wRes.GetResponseStream(); StreamReader readerPost = new StreamReader(respPostStream, Encoding.GetEncoding("EUC-KR"), true); resResult = readerPost.ReadToEnd(); } return true; } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) { var resp = (HttpWebResponse)ex.Response; if (resp.StatusCode == HttpStatusCode.NotFound) { // 예외 처리 } else { // 예외 처리 } } else { // 예외 처리 } return false; } } // 활용 예제 private void searchBoard(PortalBoard[] board, string boardId, int sPage, int ePage, string query) { MainForm.gridView.Columns[4].HeaderText = "조회수"; for (int i = 0; i < 10 * 10; i++) { board[i] = new PortalBoard(); } for (int pageNum = sPage; pageNum <= ePage; pageNum++) { byte[] b = System.Text.Encoding.GetEncoding(51949).GetBytes(query); string result = ""; foreach (byte ch in b) { result += ("%" + string.Format("{0:x2} ", ch)); // EUC-KR 인코딩 } string url = "http://portal.unist.ac.kr/EP/web/collaboration/bbs/jsp/BB_BoardLst.jsp?boardid=" + boardId + "&nfirst=" + pageNum + "&searchcondition=BULLTITLE&searchname=" + result.Replace(" ",""); if (!getResponse(url)) // HttpWebRequest 전송 return; doc = (IHTMLDocument2)new HTMLDocument(); // 파징을 위해 IHTMLDocument2 객체 생성 // IHTMLDocument1, IHTMLDocument2, IHTMLDocument3, IHTMLDocument4 // 데이터형 마다 사용할 수 있는 함수 종류 다름. msdn 참고 doc.clear(); doc.write(resResult); doc.close(); IEnumerable<ihtmlelement> titles = ElementsByClass(doc, "ltb_left"); IEnumerable<ihtmlelement> elements = ElementsByClass(doc, "ltb_center"); // 파징 후 각자 활용 } } [예제2] private HtmlAgilityPack.HtmlDocument getpage(string url ,String input) { try { Stream datastream; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); request.AllowAutoRedirect = true; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2"; request.ContentType = "application/x-www-form-urlencoded"; if (input!=null) { String postData = ""; request.Method = "POST"; if (input == "login") { postData = String.Format("username={0}&password={1}", "myusername", "mypassword"); } else if (input == "sendMessage") { //THIS IS THE LONG STRING THAT I DON'T WANT TO HARD CODE postData = String.Format("reciever={0}&sendmessage={1}", "thepersontomessage" ,this.DefaultMessage); //I am just puting two parameters for now, there should be alot } byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentLength = byteArray.Length; datastream = request.GetRequestStream(); datastream.Write(byteArray, 0, byteArray.Length); datastream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); datastream = response.GetResponseStream(); String sourceCode = ""; using (StreamReader reader = new StreamReader(datastream)) { sourceCode = reader.ReadToEnd(); } HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument(); htmlDoc.LoadHtml(sourceCode); this.cookies.Add(response.Cookies); return htmlDoc; } catch (Exception) { return null; } Dreamy의 코드 스크랩내가 모으고 내가 보는
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Designed by sketchbooks.co.kr / sketchbook5 board skin
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5