Skip to content

Commit cac8ddd

Browse files
committed
多类型复用
1 parent 946d3c6 commit cac8ddd

31 files changed

Lines changed: 1012 additions & 37 deletions

‎.idea/libraries/Maven__com_squareup_okhttp3_okhttp_3_10_0.xml‎

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/libraries/Maven__com_squareup_okio_okio_1_14_0.xml‎

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎HttpTest2/pom.xml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.zone.http</groupId>
8+
<artifactId>test2</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>com.squareup.okhttp3</groupId>
14+
<artifactId>okhttp</artifactId>
15+
<version>(insert latest version)</version>
16+
</dependency>
17+
</dependencies>
18+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.io.IOException;
2+
import okhttp3.OkHttpClient;
3+
import okhttp3.Request;
4+
import okhttp3.Response;
5+
6+
public class GetExample {
7+
OkHttpClient client = new OkHttpClient();
8+
9+
String run(String url) throws IOException {
10+
Request request = new Request.Builder()
11+
.url(url)
12+
.build();
13+
14+
try (Response response = client.newCall(request).execute()) {
15+
return response.body().string();
16+
}
17+
}
18+
19+
public static void main(String[] args) throws IOException {
20+
GetExample example = new GetExample();
21+
// String response = example.run("https://raw.github.com/square/okhttp/master/README.md");
22+
String response = example.run("https://www.baidu.com");
23+
System.out.println(response);
24+
}
25+
}

‎HttpTest3/HttpTest3.iml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
<orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.10.0" level="project" />
15+
<orderEntry type="library" name="Maven: com.squareup.okio:okio:1.14.0" level="project" />
16+
</component>
17+
</module>

‎HttpTest3/pom.xml‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.zone.http2</groupId>
8+
<artifactId>zonetest</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>1.7</source>
17+
<target>1.7</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
<dependencies>
23+
<dependency>
24+
<groupId>com.squareup.okhttp3</groupId>
25+
<artifactId>okhttp</artifactId>
26+
<version>3.10.0</version>
27+
</dependency>
28+
</dependencies>
29+
30+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.io.IOException;
2+
3+
import okhttp3.OkHttpClient;
4+
import okhttp3.Request;
5+
import okhttp3.Response;
6+
7+
public class GetExample {
8+
OkHttpClient client = new OkHttpClient();
9+
10+
String run(String url) throws IOException {
11+
Request request = new Request.Builder()
12+
.url(url)
13+
.build();
14+
15+
try (Response response = client.newCall(request).execute()) {
16+
return response.body().string();
17+
}
18+
}
19+
20+
public static void main(String[] args) throws IOException {
21+
GetExample example = new GetExample();
22+
// String response = example.run("https://raw.github.com/square/okhttp/master/README.md");
23+
String response = example.run("https://www.baidu.com");
24+
System.out.println(response);
25+
}
26+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import okhttp3.Interceptor;
2+
import okhttp3.OkHttpClient;
3+
import okhttp3.Request;
4+
import okhttp3.Response;
5+
6+
import java.io.IOException;
7+
import java.util.logging.Logger;
8+
9+
public final class LoggingInterceptors {
10+
11+
private static final Logger logger = Logger.getLogger(LoggingInterceptors.class.getName());
12+
13+
private final OkHttpClient client = new OkHttpClient.Builder()
14+
// .addInterceptor(new LoggingInterceptor())
15+
.addInterceptor(new LoggingInterceptor2())
16+
.addInterceptor(new LoggingInterceptor3())
17+
.build();
18+
19+
public void run() throws Exception {
20+
Request request = new Request.Builder()
21+
.url("https://publicobject.com/helloworld.txt")
22+
.build();
23+
24+
Response response = client.newCall(request).execute();
25+
response.body().close();
26+
}
27+
28+
private static class LoggingInterceptor implements Interceptor {
29+
@Override
30+
public Response intercept(Chain chain) throws IOException {
31+
long t1 = System.nanoTime();
32+
Request request = chain.request();
33+
logger.info(String.format("Sending request %s on %s%n%s",
34+
request.url(), chain.connection(), request.headers()));
35+
Response response = chain.proceed(request);
36+
37+
long t2 = System.nanoTime();
38+
logger.info(String.format("Received response for %s in %.1fms%n%s",
39+
request.url(), (t2 - t1) / 1e6d, response.headers()));
40+
return response;
41+
}
42+
}
43+
private static class LoggingInterceptor2 implements Interceptor {
44+
@Override
45+
public Response intercept(Chain chain) throws IOException {
46+
long t1 = System.nanoTime();
47+
Request request = chain.request();
48+
49+
request =request.newBuilder().addHeader("request2","white2").build();
50+
logger.info(String.format("Sending request %s on %s%n%s",
51+
request.url(), chain.connection(), request.headers()));
52+
53+
54+
Response response = chain.proceed(request);
55+
response.header("Accept-Ranges","black2");
56+
long t2 = System.nanoTime();
57+
logger.info(String.format("Received response for %s in %.1fms%n%s",
58+
request.url(), (t2 - t1) / 1e6d, response.headers()));
59+
return response;
60+
}
61+
}
62+
private static class LoggingInterceptor3 implements Interceptor {
63+
@Override
64+
public Response intercept(Chain chain) throws IOException {
65+
long t1 = System.nanoTime();
66+
Request request = chain.request();
67+
68+
request =request.newBuilder().addHeader("request3","white3").build();
69+
logger.info(String.format("Sending request %s on %s%n%s",
70+
request.url(), chain.connection(), request.headers()));
71+
72+
Response response = chain.proceed(request);
73+
response.header("Accept-Ranges","black3");
74+
long t2 = System.nanoTime();
75+
logger.info(String.format("Received response for %s in %.1fms%n%s",
76+
request.url(), (t2 - t1) / 1e6d, response.headers()));
77+
return response;
78+
}
79+
}
80+
81+
public static void main(String... args) throws Exception {
82+
new LoggingInterceptors().run();
83+
}
84+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package okhttp3.recipes;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
import okhttp3.WebSocket;
6+
import okhttp3.OkHttpClient;
7+
import okhttp3.Request;
8+
import okhttp3.Response;
9+
import okhttp3.WebSocketListener;
10+
import okio.ByteString;
11+
12+
public final class WebSocketEcho extends WebSocketListener {
13+
private void run() {
14+
OkHttpClient client = new OkHttpClient.Builder()
15+
.readTimeout(0, TimeUnit.MILLISECONDS)
16+
.build();
17+
18+
Request request = new Request.Builder()
19+
.url("ws://echo.websocket.org")
20+
.build();
21+
client.newWebSocket(request, this);
22+
23+
// Trigger shutdown of the dispatcher's executor so this process can exit cleanly.
24+
client.dispatcher().executorService().shutdown();
25+
}
26+
27+
@Override
28+
public void onOpen(WebSocket webSocket, Response response) {
29+
webSocket.send("Hello...");
30+
webSocket.send("...World!");
31+
webSocket.send(ByteString.decodeHex("deadbeef"));
32+
webSocket.close(1000, "Goodbye, World!");
33+
}
34+
35+
@Override
36+
public void onMessage(WebSocket webSocket, String text) {
37+
System.out.println("MESSAGE: " + text);
38+
}
39+
40+
@Override
41+
public void onMessage(WebSocket webSocket, ByteString bytes) {
42+
System.out.println("MESSAGE-Hex: " + bytes.hex());
43+
}
44+
45+
@Override
46+
public void onClosing(WebSocket webSocket, int code, String reason) {
47+
webSocket.close(1000, null);
48+
System.out.println("CLOSE: " + code + " " + reason);
49+
}
50+
51+
@Override
52+
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
53+
t.printStackTrace();
54+
}
55+
56+
public static void main(String... args) {
57+
new WebSocketEcho().run();
58+
}
59+
}

0 commit comments

Comments
 (0)