티스토리 뷰

okhttp

 

okhttp site 문서

http://square.github.io/okhttp/

 

 

사용방법 관련 블로그 포스트

https://www.codexpedia.com/android/okhttp-3-example-in-android/

 

Maven

Dependency는 아래와 같이 추가 합니다.

<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.2</version>
</dependency>

 

아래 코드는 google.com에 http request를 보내는 코드 입니다.

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class HttpCaller {
    OkHttpClient client = new OkHttpClient();

    String run(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    }
    public static void main(String[] args) throws IOException {
        String url = "http://www.google.com";
        String responseString = new HttpCaller().run(url);
        System.out.println(responseString);
    }
}

 

결과

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ko"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/logos/doodles/2018/holidays-2018-northern-hemisphere-day-2-5676669204430848-law.gif" itemprop="image"><meta 

--- 중략 ---

&#48372;&#45236;&#49464;&#50836;! #GoogleDoodle" property="og:description"><meta content="summary_large_image" property="twitter:card"><meta content="@GoogleDoodles" property="twitter:site">(function(){var r=['aa','async','ipv6','mu','sf'];google.plm(r);})();</script>     </body></html>

 

google.com에 요청을 보내고 응답을 받는 코드입니다.

 

 

OK Http안쓰고

 

[JAVA](Swing) 입력 받고 HTTP 요청 보내기 (GET, POST) (velog.io)

 

[JAVA](Swing) 입력 받고 HTTP 요청 보내기 (GET, POST)

Java Swing을 이용해 문자열을 입력 받고 해당 문자열을 HTTP 요청 보내는 과정을 설명하겠습니다.

velog.io

 

end.

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함