10BC0 homework · JavaCourse00/JavaCourseCodes@9bf5147 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9bf5147

Browse files
committed
homework
1 parent 155f2b5 commit 9bf5147

File tree

12 files changed

+258
-6
lines changed

12 files changed

+258
-6
lines changed

02nio/nio01/.classpath

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
<attribute name="test" value="true"/>
14+
</attributes>
15+
</classpathentry>
16+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
17+
<attributes>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
</attributes>
25+
</classpathentry>
26+
<classpathentry kind="output" path="target/classes"/>
27+
</classpath>

02nio/nio01/.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>nio01</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
5+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
6+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
7+
org.eclipse.jdt.core.compiler.release=disabled
8+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

02nio/nio01/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@
5959
<artifactId>netty-all</artifactId>
6060
<version>4.1.51.Final</version>
6161
</dependency>
62+
<dependency>
63+
<groupId>org.apache.httpcomponents</groupId>
64+
<artifactId>httpclient</artifactId>
65+
<version>4.5.13</version>
66+
</dependency>
6267
</dependencies>
6368

6469

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package java0.nio01.httpclient;
2+
import java.util.Map;
3+
4+
import org.apache.http.HttpEntity;
5+
import org.apache.http.client.methods.CloseableHttpResponse;
6+
import org.apache.http.client.methods.HttpGet;
7+
import org.apache.http.client.methods.HttpPost;
8+
import org.apache.http.entity.ContentType;
9+
import org.apache.http.entity.StringEntity;
10+
import org.apache.http.impl.client.CloseableHttpClient;
11+
import org.apache.http.impl.client.HttpClients;
12+
import org.apache.http.util.EntityUtils;
13+
14+
15+
/**
16+
* http客户端
17+
*
18+
*/
19+
public class HttpClient {
20+
21+
/**
22+
* post请求传输json数据
23+
*
24+
* @param url url地址
25+
* @param json json数据
26+
* @param encoding 编码方式
27+
* @param headerMap 请求头中的键值对
28+
* @return
29+
* @throws Exception
30+
*/
31+
public static String sendPostDataByJson(String url, String json, String encoding, Map<String, String> headerMap) throws Exception {
32+
String result = "";
33+
34+
// 创建httpclient对象
35+
CloseableHttpClient httpClient = HttpClients.createDefault();
36+
// 创建post方式请求对象
37+
HttpPost httpPost = new HttpPost(url);
38+
// 将头信息设置到请求对象中
39+
if(headerMap != null) {
40+
headerMap.forEach((key, value) -> {
41+
httpPost.addHeader(key, value);
42+
});
43+
}
44+
// 设置参数到请求对象中
45+
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
46+
stringEntity.setContentEncoding(encoding);
47+
httpPost.setEntity(stringEntity);
48+
// 执行请求操作,并拿到结果(同步阻塞)
49+
CloseableHttpResponse response = httpClient.execute(httpPost);
50+
// 获取结果实体
51+
// 判断网络连接状态码是否正常(0--200都数正常)
52+
// response.getStatusLine().getStatusCode() == HttpStatus.SC_OK
53+
result = EntityUtils.toString(response.getEntity(), "utf-8");
54+
// 释放链接
55+
response.close();
56+
return result;
57+
}
58+
59+
public static String sendPostDataByJson(String url, String json) throws Exception {
60+
return sendPostDataByJson(url, json, "utf-8", null);
61+
}
62+
63+
/**
64+
* post请求传输Xml数据
65+
*
66+
* @param url url地址
67+
* @param xml xml数据
68+
* @param encoding 编码方式
69+
* @return
70+
* @throws Exception
71+
*/
72+
public static String sendPostDataByXml(String url, String xml, String encoding, Map<String, String> headerMap) throws Exception {
73+
String result = "";
74+
// 创建httpclient对象
75+
CloseableHttpClient httpClient = HttpClients.createDefault();
76+
// 创建post方式请求对象
77+
HttpPost httpPost = new HttpPost(url);
78+
if(headerMap != null) {
79+
headerMap.forEach((key, value) -> {
80+
httpPost.addHeader(key, value);
81+
});
82+
}
83+
// 设置参数到请求对象中
84+
StringEntity stringEntity = new StringEntity(xml, ContentType.APPLICATION_XML);
85+
stringEntity.setContentEncoding(encoding);
86+
httpPost.setEntity(stringEntity);
87+
// 执行请求操作,并拿到结果(同步阻塞)
88+
CloseableHttpResponse response = httpClient.execute(httpPost);
89+
// 获取结果实体
90+
// 判断网络连接状态码是否正常(0--200都数正常)
91+
// response.getStatusLine().getStatusCode() == HttpStatus.SC_OK
92+
result = EntityUtils.toString(response.getEntity(), encoding);
93+
// 释放链接
94+
response.close();
95+
return result;
96+
}
97+
98+
public static String sendPostDataByXml(String url, String xml) throws Exception {
99+
return sendPostDataByXml(url, xml, "GBK", null);
100+
}
101+
102+
// GET 调用
103+
public static String getAsString(String url) throws Exception{
104+
CloseableHttpClient httpClient = HttpClients.createDefault();
105+
HttpGet httpGet = new HttpGet(url);
106+
CloseableHttpResponse response1 = null;
107+
try {
108+
response1 = httpClient.execute(httpGet);
109+
System.out.println(response1.getStatusLine());
110+
HttpEntity entity1 = response1.getEntity();
E839 111+
return EntityUtils.toString(entity1, "UTF-8");
112+
} finally {
113+
if (null != response1) {
114+
response1.close();
115+
}
116+
}
117+
}
118+
119+
public static void main(String[] args) throws Exception {
120+
System.out.println(getAsString("http://localhost:8088/api/hello"));
121+
}
122+
123+
}

02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.netty.handler.codec.http.FullHttpResponse;
1010
import io.netty.handler.codec.http.HttpUtil;
1111
import io.netty.util.ReferenceCountUtil;
12+
import java0.nio01.httpclient.HttpClient;
1213

1314
import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION;
1415
import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
@@ -44,12 +45,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
4445
private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
4546
FullHttpResponse response = null;
4647
try {
47-
String value = null; // "hello,kimmking"; // 对接上次作业的httpclient或者okhttp请求另一个url的响应数据
48-
49-
// httpGet ... http://localhost:8801
50-
// 返回的响应,"hello,nio";
51-
// value = reponse....
52-
48+
String value = HttpClient.getAsString("http://localhost:8088/api/hello"); //本地网关url
5349
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8")));
5450
response.headers().set("Content-Type", "application/json");
5551
response.headers().setInt("Content-Length", response.content().readableBytes());

02nio/nio02/.classpath

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
<attribute name="test" value="true"/>
14+
</attributes>
15+
</classpathentry>
16+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
17+
<attributes>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
</attributes>
25+
</classpathentry>
26+
<classpathentry kind="output" path="target/classes"/>
27+
</classpath>

02nio/nio02/.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>netty-gateway</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding/<project>=UTF-8

0 commit comments

Comments
 (0)
0