16 Şubat 2023 Perşembe

OKHttpUtil

Giriş
Açıklaması şöyle
In the Java world, Apache’s HttpClient has always been the dominant Http client before, but because the package is relatively large and the API is difficult to use, it is not used in many scenarios. The emerging OkHttp and Jodd-http are easy to use, but when faced with some scenarios, there are still some learning costs.

Many times, we want to pursue a lightweight Http client and pursue simplicity and ease of use. OKHttp is a set of dependent libraries for processing HTTP network requests. It was designed and developed by Square and is open source. It can currently be used in Java and Kotlin.

For Android Apps, OkHttp now occupies almost all network request operations, and it is also a necessary choice for server-side requests to external interfaces.
Maven
Şu satırı dahil ederiz
<dependency>
    <groupId>io.github.admin4j</groupId>
    <artifactId>http</artifactId>
    <version>0.4.0</version>
</dependency>
HttpUtil Sınıfı
Açıklaması şöyle
For OKHttp, OkHttpUtil has made a layer of encapsulation, making Http requests extremely simple.
Açıklaması şöyle
- Automatically judge whether to request HTTP or HTTPS according to the URL, no need to write redundant code separately.
- By default, cookies are automatically recorded. For example, simulated login can be implemented, that is, subsequent requests after the first visit to the login URL are the login status.
- Automatically identify 304 jumps and make a second request
- Support proxy configuration
- Support referer configuration
- Support User-Agent configuration
- Automatically identify and decompress Gzip format to return content
- Support springboot configuration files
- Minimalist encapsulation call
down metodu - File Download
Şöyle yaparız
HttpUtil.down("https://gitee.com/admin4j/common-http","path/");
execute metodu
Örnek - get
Şöyle yaparız
# get
Response response=HttpRequest.get("https://search.gitee.com/?skin=rec&type=repository")
.queryMap(q,admin4j)
.header(HttpHeaderKey.USER_AGENT,admin4j)
.execute();

System.out.println(response = +response);
Örnek - post
Şöyle yaparız
# post form
Response response=HttpRequest.get("http://192.168.1.13:9100/auth/login")
.queryMap(q,admin4j)
.header(HttpHeaderKey.USER_AGENT,admin4j)
.form(username,admin)
.form(password,admin123)
.execute();

System.out.println(response = +response);
get metodu
Şöyle yaparız
Response response = HttpUtil.get("https://github.com/search", Pair.of(q, okhttp));
System.out.println(response =  + response);
post metodu
Şöyle yaparız
# JSON 
Response post = HttpUtil.post("https://...", "{...}");
System.out.println(post =  + post);
postForm metodu
Şöyle yaparız
# form 
Map<String, Object> formParams = new HashMap<>(16);
formParams.put("username", "admin");
formParams.put("password", "admin123");
Response response = HttpUtil.postForm("http://192.168.1.13:9100/auth/login",
                formParams
);
System.out.println(response =  + response);
upload metodu - File Upload
Şöyle yaparız
File file=new File(C:\\Sql.txt);
Map<String, Object> formParams=new HashMap<>();
formParams.put("key",test);
formParams.put("file",file);
formParams.put("token","...");
Response response=HttpUtil.upload("https://upload.qiniup.com/",formParams);
System.out.println(response);
HttpJsonUtil Sınıfı
Açıklaması şöyle
If the return format is JSON, you can use it to HttpJsonUtil automatically return JsonObject
get metodu
Şöyle yaparız
JSONObject object=HttpJsonUtil.get("https://github.com/search",
  Pair.of(q,http),
  Pair.of(username,agonie201218));

System.out.println(object = +object);
OkHttpClient.Builder Sınıfı
Örnek
Şöyle yaparız
@EnableRetrofitClients(basePackages = "com.oguzderici.retrofitdemo.clients")
@Configuration
public class RetrofitConfiguration {

  @Bean
  @LoadBalanced
  public OkHttpClient.Builder okHttpClientBuilder() {
    return new OkHttpClient.Builder()
      .addInterceptor(new HttpLoggingInterceptor()
                        .setLevel(HttpLoggingInterceptor.Level.BASIC));
    }
}


Hiç yorum yok:

Yorum Gönder