티스토리 뷰

라이믹스나 xe 게시판을 파싱할 경우 로그인 한 사용자만 볼 수 있게 권한을 설정한 경우가 있다. 그럴경우 파싱하는 방법을 알아본다.

또한 게시판에 자동 글 쓰기 하는 방법도 알아본다. 마찬가지로 글 쓰기도 로그인 한 사용자로 관리자가 권한 설정을 해 놓았을 때도 글 쓰기가 가능하다.

쎌레니움으로도 해보았지만 그냥 코드로 하는게 브라우저 띄우지 않아도 되서 편한것 같다.

 

package rxwrite;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 * notice 게시판을 권한설정에서 열람을 로그인 사용자로 바꾼다.
 * 그러면 로그인 안 한 사용자는 목록은 보이지만 제목 클릭시 내용은 보이지 않는다.
 * 이러한 환경에서 파싱하는 방법이다.
 * 
 * 또한 글쓰기도 로그인한 사용자로 설정되어 있는 게시판에 글쓰기를 하는 예제이다.
 *
 */
public class RxWrite {

	// /rx/notice/84 바로 호출하면 403 (Forbidden) 에러.
	public static void main(String[] args) throws IOException {
		// User Agent.
		String userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36";
		
		// 전송할 폼 데이터
		Map<String,String> data=new HashMap<>();
		data.put("error_return_url", "/rx/index.php?mid=index&act=dispMemberLoginForm");
		data.put("mid", "index");
		data.put("act", "procMemberLogin");
		data.put("success_return_url", "http://localhost/rx/index.php?mid=index&amp;act=dispMemberLoginForm");
		data.put("xe_validator_id", "layouts/xedition/layout/1");
		data.put("user_id", "메일주소");
		data.put("password", "비밀번호");
		
		// 로그인(POST)
		Connection.Response res=Jsoup.connect("http://localhost/rx/index.php?act=procMemberLogin")
				.userAgent(userAgent)
				.timeout(3000)
				.header("Origin", "http://localhost")
				//.header("Referer", "http://localhost/rx/index.php?mid=index&amp;act=dispMemberLoginForm")
				.data(data)
				.method(Connection.Method.POST)
				.execute();
		
		String sessionId = res.cookie("PHPSESSID");
		//Document doc=res.parse();
		//System.out.println(doc);
		
		// 전송할 폼 데이터
		data=new HashMap<>();
		data.put("mid", "notice");
		data.put("title", "title");
		data.put("content", "content");
			
		res=Jsoup.connect("http://localhost/rx/index.php?act=procBoardInsertDocument")
			.userAgent(userAgent)
			.timeout(3000)
			.header("Origin", "http://localhost")
			.data(data)
			.method(Connection.Method.POST)
			.cookie("PHPSESSID", sessionId)
			.execute();

		// 글쓰기 종료
		
		// 열람 로그인 사용자로 되어 있는 게시판의 글을 보여준다.
		String url = "http://localhost/rx/notice/165";
		Document doc=Jsoup.connect(url)
				.cookie("PHPSESSID", sessionId)
				.get();
		System.out.println(doc);
		
		//만약 .cookie를 안한다면 
		//Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL=[http://localhost/rx/notice/165]
		//에러가 발생한다.
				
	}

}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함