Browse Source

okhttp

pull/1/head
woniu 2 years ago
parent
commit
5b11a96ba2
  1. 8
      httpdemo/.idea/.gitignore
  2. 19
      httpdemo/.idea/compiler.xml
  3. 6
      httpdemo/.idea/encodings.xml
  4. 25
      httpdemo/.idea/jarRepositories.xml
  5. 14
      httpdemo/.idea/misc.xml
  6. 2
      httpdemo/httpdemo.iml
  7. 62
      httpdemo/pom.xml
  8. 14
      httpdemo/src/main/java/com/woniu/httpdemo/HttpApplication.java
  9. 48
      httpdemo/src/main/java/com/woniu/httpdemo/configration/HttpConfig.java
  10. 80
      httpdemo/src/main/java/com/woniu/httpdemo/configration/MainTest.java
  11. BIN
      httpdemo/target/classes/com/woniu/httpdemo/HttpApplication.class
  12. BIN
      httpdemo/target/classes/com/woniu/httpdemo/configration/HttpConfig$ProxyConfig.class
  13. BIN
      httpdemo/target/classes/com/woniu/httpdemo/configration/HttpConfig.class
  14. BIN
      httpdemo/target/classes/com/woniu/httpdemo/configration/MainTest.class

8
httpdemo/.idea/.gitignore

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/../../../:\httpdemo\.idea/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

19
httpdemo/.idea/compiler.xml

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="httpdemo" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="httpdemo" options="-parameters" />
</option>
</component>
</project>

6
httpdemo/.idea/encodings.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
</component>
</project>

25
httpdemo/.idea/jarRepositories.xml

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://maven.aliyun.com/nexus/content/repositories/central/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

14
httpdemo/.idea/misc.xml

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

2
httpdemo/httpdemo.iml

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />

62
httpdemo/pom.xml

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.woniu</groupId>
<artifactId>httpdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>httpdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>io.github.admin4j</groupId>
<artifactId>http</artifactId>
<version>0.4.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

14
httpdemo/src/main/java/com/woniu/httpdemo/HttpApplication.java

@ -0,0 +1,14 @@
package com.woniu.httpdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HttpApplication {
public static void main(String[] args) {
SpringApplication.run(HttpApplication.class, args);
}
}

48
httpdemo/src/main/java/com/woniu/httpdemo/configration/HttpConfig.java

@ -0,0 +1,48 @@
package com.woniu.httpdemo.configration;
import lombok.Data;
import java.net.Proxy;
public class HttpConfig {
/**
* 读取超时时间
*/
private long readTimeout = 30;
/**
* 链接超时时间
*/
private long connectTimeout = 30;
private boolean followRedirects = false;
/**
* 最大的连接数
*/
private int maxIdleConnections = 5;
/**
* 最大的kepAlive 时间
*/
private long keepAliveDuration = 5;
private String userAgent = "OKHTTP";
/**
* 是否支持cookie
*/
private boolean cookie = false;
private ProxyConfig proxy;
@Data
public static class ProxyConfig {
private Proxy.Type type = Proxy.Type.HTTP;
private String host;
private Integer port = 80;
private String userName;
private String password;
}
}

80
httpdemo/src/main/java/com/woniu/httpdemo/configration/MainTest.java

@ -0,0 +1,80 @@
package com.woniu.httpdemo.configration;
import com.alibaba.fastjson.JSONObject;
import io.github.admin4j.http.HttpRequest;
import io.github.admin4j.http.core.HttpHeaderKey;
import io.github.admin4j.http.core.Pair;
import io.github.admin4j.http.util.HttpJsonUtil;
import io.github.admin4j.http.util.HttpUtil;
import okhttp3.Response;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* 一行代码搞定Http请求真强!
* OKHttp OkHttpUtil做了一层封装使Http请求变得无比简单
*/
public class MainTest {
public static void main(String[] args) {
//GET
Response response = HttpUtil.get("https://github.com/search", Pair.of("q", "okhttp"));
System.out.println("response = " + response);
//JSON 格式的body
okhttp3.Response post = HttpUtil.post("https://oapi.dingtalk.com/robot/send?access_token=27f5954ab60ea8b2e431ae9101b1289c138e85aa6eb6e3940c35ee13ff8b6335",
"{\"msgtype\": \"text\",\"text\": {\"content\":\"【反馈提醒】我就是我, 是不一样的烟火\"}}");
System.out.println("post = " + post);
// form 请求
Map<String, Object> formParams = new HashMap<>(16);
formParams.put("username", "admin");
formParams.put("password", "admin123");
HttpUtil.postForm("http://192.168.1.13:9100/auth/login",
formParams
);
//返回格式为JSON的 可以使用 HttpJsonUtil 自动返回JsonObject
JSONObject object = HttpJsonUtil.get("https://github.com/search",
Pair.of("q", "http"),
Pair.of("username", "agonie201218"));
System.out.println("object = " + object);
//文件上传
File file = new File("C:\\Users\\andanyang\\Downloads\\Sql.txt");
Map<String, Object> formParams = new HashMap<>();
formParams.put("key", "test");
formParams.put("file", file);
formParams.put("token", "WXyUseb-D4sCum-EvTIDYL-mEehwDtrSBg-Zca7t:qgOcR2gUoKmxt-VnsNb657Oatzo=:eyJzY29wZSI6InpoYW56aGkiLCJkZWFkbGluZSI6MTY2NTMwNzUxNH0=");
Response response = HttpUtil.upload("https://upload.qiniup.com/", formParams);
System.out.println(response);
//下载文件
HttpUtil.down("https://gitee.com/admin4j/common-http", "path/");
//HttpRequest 链式请求
// # 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);
//# 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);
}
}

BIN
httpdemo/target/classes/com/woniu/httpdemo/HttpApplication.class

Binary file not shown.

BIN
httpdemo/target/classes/com/woniu/httpdemo/configration/HttpConfig$ProxyConfig.class

Binary file not shown.

BIN
httpdemo/target/classes/com/woniu/httpdemo/configration/HttpConfig.class

Binary file not shown.

BIN
httpdemo/target/classes/com/woniu/httpdemo/configration/MainTest.class

Binary file not shown.
Loading…
Cancel
Save