pom.xml
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
블라블라.java
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public boolean 함수명(파트너VO partnerInfo) {
logger.debug("registEDM: {}", partnerInfo.getId());
OkHttpClient client = new OkHttpClient();
String 아이디 = 아이디;
okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
// post 방식으로 요청시 body 필요함
RequestBody body = RequestBody.create(mediaType,
String.format("{\"Data\":{\"Email\":\"%s\",\"FirstName\":\"%s\",\"Field21\":\"%s\",\"EmailPerm\":\"1\"}}",
partnerInfo.getId(), partnerInfo.getUserName(), partnerInfo.getPartnerCd().toString()));
//요청
Request request = new Request.Builder()
.url(String.format("https://요청url", 아이디))
.post(body).addHeader("AuthToken", 토큰)
.addHeader("Content-Type", "application/json")
.addHeader("Cache-Control", "no-cache")
.build();
try {
//응답
Response response = client.newCall(request).execute();
} catch (IOException e) {
logger.error("EDM error : {}", e.getMessage());
e.printStackTrace();
return false;
}
return true;
}
블라블라2.java
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
private String 함수명(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
client.setFollowRedirects(false);
client.setConnectTimeout(socketTimeout, TimeUnit.MILLISECONDS);
Request request = new Request.Builder()
.url(url).get()
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "토큰")
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36")
.build();
try {
Response response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
throw e;
}
}
블라블라3.java
private String 함수명(String url) throws IOException
{
OkHttpClient client = new OkHttpClient();
client.setFollowRedirects(false);
client.setConnectTimeout(socketTimeout, TimeUnit.MILLISECONDS);
Request request = new Request.Builder()
.url(url)
.get()
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "토큰")
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36")
.build();
try {
Response response = client.newCall(request).execute();
System.out.println("response.body().string(): "+response.body().string());
return response.body().string();
}
catch (IOException e) {
throw e;
}
}
OkHttpClient 로 요청하니 갑자기 안된다.
응답을 확인하니
Response{protocol=http/1.1, code=303, message=See Other, url=https://youtu.be/ytL53aHqarM}
:요청된 URL이 잠시 다른 URL로 바뀐 것을 알림. (Location: header로 지정된) 바뀐 URL은 GET method로 접근해야 함
봇이라고 감지한건가? 그래서 header부분을 지웠는데도 안된다..
그래서 Jsoup으로는 접근이 되나 똑같은 환경으로 테스트했더니 됨... 뭐지???
2시간 만에 해결 방법을 찾았다.
응답 받은 것처럼 redirect되기 때문에
client.setFollowRedirects(false); 이게 true로 바꿔주면 body를 읽어올 수 있었다. ㅠㅠㅠ
Jsoup블라블라1.java
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
private 리턴DTO 함수명(리턴DTO VO) throws IOException
{
URI uri = UriComponentsBuilder.fromUriString(VO.getMediaUrl()).build().encode().toUri();
Document doc = null;
try {
doc = Jsoup.connect(uri.toString())
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36")
.timeout(socketTimeout).get();
}
catch (Exception e) {
if (e instanceof HttpStatusException) {
try {
Thread.sleep(1000);
}
catch (Exception sube) {
}
doc = Jsoup.connect(uri.toString()).timeout(socketTimeout).get();
}
}
Elements scriptEle = doc.select("script");
....
}
'기술 블로그 (Tech Blog) > 검색용 개발 블로그' 카테고리의 다른 글
`eslint 'component/**/*' 'pages/**/*' 'payload/**/*' '*.ts' --fix` (0) | 2021.09.11 |
---|---|
git repository 이름 변경 후 올리기 (0) | 2021.09.11 |
[javascirpt] object ==, === 비교 (0) | 2021.09.07 |
error Unexpected token < in JSON at position 0 while parsing near '<!DOCTYPE html> (0) | 2021.09.05 |
mysql select 자동순번 (0) | 2021.09.03 |