File tree Expand file tree Collapse file tree 3 files changed +111
-0
lines changed
02nio/nio02/src/main/java/io/github/kimmking/gateway Expand file tree Collapse file tree 3 files changed +111
-0
lines changed Original file line number Diff line number Diff line change 1+ package io .github .kimmking .gateway .client ;
2+
3+ import org .apache .http .HttpEntity ;
4+ import org .apache .http .HttpStatus ;
5+ import org .apache .http .client .methods .CloseableHttpResponse ;
6+ import org .apache .http .client .methods .HttpGet ;
7+ import org .apache .http .impl .client .CloseableHttpClient ;
8+ import org .apache .http .impl .client .HttpClients ;
9+ import org .apache .http .util .EntityUtils ;
10+
11+ import java .io .IOException ;
12+ import java .util .Objects ;
13+
14+ public class HttpClientTest {
15+
16+ public static void main (String [] args ) throws IOException {
17+
18+ CloseableHttpResponse response = null ;
19+ HttpGet get = null ;
20+
21+ CloseableHttpClient client = HttpClients .createDefault ();
22+
23+ try {
24+ StringBuilder url = new StringBuilder ("http://localhost:8888" );
25+ get = new HttpGet (url .toString ());
26+
27+ response = client .execute (get );
28+ if (response .getStatusLine ().getStatusCode () == HttpStatus .SC_OK ) {
29+ HttpEntity entity = response .getEntity ();
30+ String html = EntityUtils .toString (entity , "utf-8" );
31+ System .out .println (html );
32+ }
33+ } catch (Exception e ){
34+ e .printStackTrace ();
35+ }
36+ finally {
37+ if (Objects .nonNull (response )) {
38+ response .close ();
39+ }
40+
41+ if (Objects .nonNull (get )) {
42+ get .releaseConnection ();
43+ }
44+
45+ client .close ();
46+ }
47+
48+ }
49+
50+ }
Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ public void run() throws Exception {
5353 System .out .println ("开启netty http服务器,监听地址和端口为 http://127.0.0.1:" + port + '/' );
5454 ch .closeFuture ().sync ();
5555 } finally {
56+ System .out .println ("关闭线程组" );
5657 bossGroup .shutdownGracefully ();
5758 workerGroup .shutdownGracefully ();
5859 }
Original file line number Diff line number Diff line change 1+ package io .github .kimmking .gateway .server ;
2+
3+ import java .io .IOException ;
4+ import java .io .PrintWriter ;
5+ import java .net .ServerSocket ;
6+ import java .net .Socket ;
7+
8+ public class HttpServerTest {
9+
10+ static int port = 8801 ;
11+
12+ public static void main (String [] args ) throws IOException {
13+
14+ if (args != null && args .length > 0 ) {
15+ try {
16+ port = Integer .valueOf (args [0 ]);
17+ } catch (NumberFormatException e ) {
18+ e .printStackTrace ();
19+ }
20+
21+ }
22+
23+ ServerSocket serverSocket = new ServerSocket (port );
24+
25+ while (true ){
26+ try {
27+ Socket socket = serverSocket .accept ();
28+ service (socket );
29+ } catch (IOException e ) {
30+ e .printStackTrace ();
31+ }
32+ }
33+
34+ }
35+
36+ private static void service (Socket socket ) {
37+
38+ try {
39+ PrintWriter printWriter = new PrintWriter (socket .getOutputStream (),true );
40+ printWriter .println ("HTTP/1.1 200 OK" );
41+ printWriter .println ("Content-Type:text/html;charset=utf-8" );
42+ String body = "hello,nio" + port ;
43+ printWriter .println ("Content-Length:" + body .getBytes ().length );
44+ printWriter .println ();
45+
46+ printWriter .write (body );
47+ printWriter .close ();
48+
49+ //这里主要是为了让httpClient可以在server的socket关闭前,拿到返回的结果
50+ Thread .sleep (10 );
51+
52+ socket .close ();
53+
54+ } catch (IOException | InterruptedException e ) {
55+ e .printStackTrace ();
56+ }
57+
58+ }
59+
60+ }
You can’t perform that action at this time.
0 commit comments