OkHttp general and simple encapsulation

CommonOkHttp--Simple encapsulation of common OkHttp

A simple package
github address  of OkHttp for Java applications (non-Android) https://keran213539.github.io/CommonOkHttp/
maven introduced

<dependency>
    <groupId>com.github.keran213539</groupId>
    <artifactId>commonOkHttp</artifactId>
    <version>0.2</version>
</dependency>

After tossing for a day, I finally submitted it to the maven central repository. I will write a tutorial on how to submit it to the manven central repository later.

background

OkHttp, an alternative to Apache httpClient that is very popular now, said that the replacement may not be suitable, at least it is the same type of library.
Recently, I want to learn to use it, and find a general tool to use in new projects (the old project uses httpClient) .Since it is so popular now, the general tools written by others should be easy to find (well, bring ideas), after a search, I did find a lot, but they are all Android, and Android components are encapsulated in a lot , and download, download progress... The server can't use it at all... @%#$%#$ Well, if you can't find it, write it yourself...
Here's a good Android http Framework  OkGo , I refer to the https tool and other parts of the code

how to use

via spring factory

Refer to com.github.keran213539.commonOkHttp.test.TestWithSpring, add in spring configuration file:

    <bean id="httpClientDefaultHttps" class="com.github.keran213539.commonOkHttp.CommonOkHttpClientFactory" />
    
    <bean id="httpClientNotSafe" class="com.github.keran213539.commonOkHttp.CommonOkHttpClientFactory">
        <property name="unSafe" value="true" />
    </bean>
    
    <bean id="httpClientCustomCertificate" class="com.github.keran213539.commonOkHttp.CommonOkHttpClientFactory">
        <property name="checkHostname" value="false" />
        <property name="certificateFilePaths">
            <list>
                <value>classpath*:cers/jianshu.cer</value>
            </list>
        </property>
    </bean>

Get the corresponding bean usage through spirng

by Builder

参考 com.github.keran213539.commonOkHttp.test.TestWithBuilder

    // 默认CA方式
    CommonOkHttpClient defaultHttps = new CommonOkHttpClientBuilder().build();
    // 不安全
    CommonOkHttpClient httpClientNotSafe = new CommonOkHttpClientBuilder().unSafe(true).build();
    // 指定信任证书
    List<URL> certificateFilePaths = new ArrayList<>();
    certificateFilePaths.add(ResourceUtils.getURL("classpath:cers/jianshu.cer"));
    CommonOkHttpClient httpClientCustomCertificate = new CommonOkHttpClientBuilder().checkHostname(false).certificateFilePaths(certificateFilePaths).build();

Method description

String get(String url, IAsyncCallback callback)

发送 get 请求, 有 callback为异步,callback传null为同步;异步时返回null

String post(String url, IAsyncCallback callback)

使用无参方式发送post请求, 有 callback为异步,callback传null为同步;异步时返回null

String post(String url,String jsonStr, IAsyncCallback callback)

使用json方式发送post请求, 有 callback为异步,callback传null为同步;异步时返回null

String post(String url, Map<String, String> prarm, IAsyncCallback callback)

使用传统参数方式发送post请求, 有 callback为异步,callback传null为同步;异步时返回null

<T extends UploadFileBase> String post(String url, Map<String, String> prarm, List<T> files, IAsyncCallback callback)

文件上传(支持多文件)

About testing

In addition to the method of encrypting and decrypting data with the specified certificate, there is no conditional test for the time being. Others have been tested. At present, it is known that the refund of WeChat payment needs to use this method. The previous company made WeChat payment and used Apache HttpClient at that time. Later, I will find an opportunity to test
the JUnit test and delete some of the code that did not find a suitable test environment on the public network (some of the test environments I simulated in the local machine), such as using a self-signed certificate, in an unsafe way, upload Wait, you can self-signed a certificate and deploy it as
the part of the custom trust certificate in the https self-test code, delete some certificates of other environments, and only keep the certificate of Jianshu as a code example

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326061016&siteId=291194637