Code Monkey home page Code Monkey logo

simple-http's Introduction

Travis-CI MAVEN author JDK LICENSE

简介

抽取一个简单 HTTP 的通用接口,底层实现根据具体引入依赖指定。

<dependency>
  <groupId>com.xkcoding.http</groupId>
  <artifactId>simple-http</artifactId>
  <version>1.0.5</version>
</dependency>

特点

  • 默认会按照下面的优先级自行寻找底层实现,java 11 HttpClient -> OkHttp3 -> apache HttpClient -> hutool-http
  • 也可以自行实现 com.xkcoding.http.support.Http 接口,通过 HttpUtil.setHttp(new MyHttpImpl()) 设置进来
  • 可以配置超时时间及代理
HttpUtil.setConfig(HttpConfig.builder()
			.timeout(Constants.DEFAULT_TIMEOUT)
			.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)))
			.build());
SimpleHttpResponse response = HttpUtil.get("https://www.google.com");
System.out.println("code = " + response.getCode());
System.out.println("body = " + response.getBody());

TODO

simple-http's People

Contributors

chunmenglu avatar dependabot[bot] avatar mvbbb avatar xkcoding avatar zhangyd-c avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

simple-http's Issues

com.xkcoding.http.exception.SimpleHttpException: java.net.http.HttpConnectTimeoutException: HTTP connect timed out

在JustAuth中报错timeout。
com.xkcoding.http.HttpUtil 创建的http实现内中都写死timeout为3000导致集成国外第三方网站全部超时,建议提供timeout参数供配置。

错误日志:
2020-04-25 16:57:35 http-nio-8085-exec-2 me.zhyd.oauth.log.Log(error:45) [ERROR] - Failed to login with oauth authorization.
com.xkcoding.http.exception.SimpleHttpException: java.net.http.HttpConnectTimeoutException: HTTP connect timed out
at com.xkcoding.http.support.java11.HttpClientImpl.exec(HttpClientImpl.java:56)
at com.xkcoding.http.support.java11.HttpClientImpl.post(HttpClientImpl.java:169)
at com.xkcoding.http.support.java11.HttpClientImpl.post(HttpClientImpl.java:141)
at com.xkcoding.http.support.java11.HttpClientImpl.post(HttpClientImpl.java:128)
at com.xkcoding.http.HttpUtil.post(HttpUtil.java:120)
at me.zhyd.oauth.request.AuthDefaultRequest.doPostAuthorizationCode(AuthDefaultRequest.java:213)
at me.zhyd.oauth.request.AuthGithubRequest.getAccessToken(AuthGithubRequest.java:34)
at me.zhyd.oauth.request.AuthDefaultRequest.login(AuthDefaultRequest.java:79)
at org.openmall.mall.portal.ums.controller.RestAuthController.login(RestAuthController.java:48)

对应需要修改代码:
com.xkcoding.http.HttpUtil
static {
Http defaultProxy = null;
ClassLoader classLoader = HttpUtil.class.getClassLoader();
if (ClassUtil.isPresent("java.net.http.HttpClient", classLoader)) {
defaultProxy = new HttpClientImpl();
} else if (ClassUtil.isPresent("okhttp3.OkHttpClient", classLoader)) {
defaultProxy = new OkHttp3Impl();
} else if (ClassUtil.isPresent("org.apache.http.impl.client.HttpClients", classLoader)) {
defaultProxy = new com.xkcoding.http.support.httpclient.HttpClientImpl();
} else if (ClassUtil.isPresent("cn.hutool.http.HttpRequest", classLoader)) {
defaultProxy = new HutoolImpl();
}

    proxy = (Http)defaultProxy;
}

HttpClientImpl:
public HttpClientImpl() {
this(HttpClient.newBuilder().connectTimeout(Duration.ofMillis(3000L)).build());
}

OkHttp3Impl:
public OkHttp3Impl() {
this((new OkHttpClient()).newBuilder().connectTimeout(Duration.ofMillis(3000L)).writeTimeout(Duration.ofMillis(3000L)).readTimeout(Duration.ofMillis(3000L)).build());
}

HutoolImpl:
request = request.timeout(3000);

关于选择哪一种默认实现

您好,在读过您的源码之后看到源码中已经配置了OkHttp3、Apache 的 HttpClient、hutool-http的依赖,那在引入此项目之后这几种依赖不就都会被引入了吗,没能理解这样的话优先级的作用在哪

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.