package main;
import okhttp3.*;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
// OKHttp Client를 통한 호스트 단위 캐쉬 관리
// Referenced by shy.kim@samsung.com
public class Main {
public static void main(String[] args) throws Exception{
// 호스트
String host = "http://70.50.183.49:8081";
// 호스트 단위 캐쉬 관리 설정
OkHttpClient client = new OkHttpClient().newBuilder()
.cookieJar(new CookieJar() {
// 캐쉬 저장소
private final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();
// "Set-Cookie" 응답 헤더의 값을 캐쉬로 저장
@Override
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> cookies) {
cookieStore.put(httpUrl.host(), cookies);
}
// "Cookie" 요청 헤더의 값을 캐쉬 저장소의 값으로 설정
@Override
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
List<Cookie> cookies = cookieStore.get(httpUrl.host());
if (cookies != null) System.out.println(cookies.toString());
return cookies != null ? cookies : new ArrayList<Cookie>();
}
})
.build();
// 사용자 인증
String authentication_url = host + "/testnet/authentication";
String userId = "rolroralra@naver.com";
String passwd = "rlatlsdud1!";
RequestBody authentication_body = RequestBody.create(
MediaType.parse("application/json"),
new JSONObject().put("username", userId).put("password", passwd).toString()
);
Request authentication_request = new Request.Builder()
.addHeader("Content-Type", "application/json")
.addHeader("Connector", "nca") //nca or hlf or eth
.url(authentication_url)
.post(authentication_body)
.build();
Response response = client.newCall(authentication_request).execute();
System.out.println(new JSONObject(response.body().string()));
// Digital-Stamping
String digitalstamping_url = host + "/core/v2/digital-stampings";
RequestBody digitalstamping_body = RequestBody.create(
MediaType.parse("application/json"),
new JSONObject().put("hash", "test_hash" ).toString()
);
Request digitalstamping_request = new Request.Builder()
.addHeader("Content-Type", "application/json")
.addHeader("Connector", "nca") //nca or hlf or eth
.url(digitalstamping_url)
.post(digitalstamping_body)
.build();
response = client.newCall(digitalstamping_request).execute();
JSONObject responseJson = new JSONObject(response.body().string());
/* Do something with the response */
System.out.println(responseJson);
}
}
Last active
July 28, 2020 00:55
-
-
Save rolroralra/9725e385618aabd2cebe5d09a8b7f7f8 to your computer and use it in GitHub Desktop.
OkHttpClient
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment