File tree 15 files changed +147
-257
lines changed
examples/src/main/java/com/packt/learnjava/ch11_network
java/com/packt/learnjava/network/http
src/main/java/com/packt/learnjava/network/http
java/com/packt/learnjava/network/http
15 files changed +147
-257
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ package com .packt .learnjava .ch11_network ;
2
+
3
+ import java .io .InputStream ;
4
+ import java .net .URL ;
5
+
6
+ public class UrlFileReader {
7
+ public static void main (String [] args ) throws Exception {
8
+ try {
9
+ ClassLoader classLoader = Thread .currentThread ().getContextClassLoader ();
10
+ String file = classLoader .getResource ("hello.txt" ).getFile ();
11
+ URL url = new URL ("file:" + file );
12
+ try (InputStream is = url .openStream ()){
13
+ int data = is .read ();
14
+ while (data != -1 ){
15
+ System .out .print ((char ) data ); //prints: Hello!
16
+ data = is .read ();
17
+ } }
18
+ } catch (Exception e ) {
19
+ e .printStackTrace ();
20
+ }
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ package com .packt .learnjava .ch11_network ;
2
+
3
+ import java .io .BufferedReader ;
4
+ import java .io .InputStream ;
5
+ import java .io .InputStreamReader ;
6
+ import java .io .OutputStream ;
7
+ import java .io .OutputStreamWriter ;
8
+ import java .net .URL ;
9
+ import java .net .URLConnection ;
10
+
11
+ public class UrlPost {
12
+ public static void main (String [] args ) {
13
+ try {
14
+ URL url = new URL ("http://localhost:3333/something" );
15
+ URLConnection conn = url .openConnection ();
16
+ conn .setRequestProperty ("Method" , "POST" );
17
+ conn .setRequestProperty ("User-Agent" , "Java client" );
18
+ conn .setDoOutput (true );
19
+ try (OutputStream os = conn .getOutputStream ();
20
+ OutputStreamWriter osw = new OutputStreamWriter (os )) {
21
+ osw .write ("parameter1=value1¶meter2=value2" );
22
+ osw .flush ();
23
+ }
24
+ try (InputStream is = conn .getInputStream ();
25
+ BufferedReader br = new BufferedReader (new InputStreamReader (is ))) {
26
+ String line ;
27
+ while ((line = br .readLine ()) != null ) {
28
+ System .out .println (line );
29
+ }
30
+ }
31
+ } catch (Exception ex ) {
32
+ ex .printStackTrace ();
33
+ }
34
+ }
35
+
36
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ package com .packt .learnjava .ch11_network ;
2
+
3
+ import java .io .BufferedReader ;
4
+ import java .io .InputStream ;
5
+ import java .io .InputStreamReader ;
6
+ import java .net .URL ;
7
+ import java .net .URLConnection ;
8
+
9
+ public class UrlSiteReader {
10
+ public static void main (String [] args ) {
11
+ /* Warning!
12
+ * If it is invoked too many times,
13
+ * Google may block your IP address.
14
+ */
15
+ try {
16
+ URL url = new URL ("https://www.google.com/search?q=Java&num=10" );
17
+ System .out .println (url .getPath ()); //prints: /search
18
+ System .out .println (url .getFile ()); //prints: /search?q=Java&num=10
19
+ URLConnection conn = url .openConnection ();
20
+ conn .setRequestProperty ("Accept" , "text/html" );
21
+ conn .setRequestProperty ("Connection" , "close" );
22
+ conn .setRequestProperty ("Accept-Language" , "en-US" );
23
+ conn .setRequestProperty ("User-Agent" , "Mozilla/5.0" );
24
+ try (InputStream is = conn .getInputStream ();
25
+ BufferedReader br = new BufferedReader (new InputStreamReader (is ))){
26
+ String line ;
27
+ while ((line = br .readLine ()) != null ){
28
+ System .out .println (line );
29
+ }
30
+ }
31
+ } catch (Exception e ) {
32
+ e .printStackTrace ();
33
+ } }
34
+
35
+ }
Load Diff This file was deleted.
Load Diff This file was deleted.
Load Diff This file was deleted.
Load Diff This file was deleted.
Load Diff This file was deleted.
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 4
4
xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5
5
<modelVersion >4.0.0</modelVersion >
6
6
7
- <groupId >com.packt.learnjava.network.udp </groupId >
8
- <artifactId >udpclient </artifactId >
7
+ <groupId >com.packt.learnjava</groupId >
8
+ <artifactId >server </artifactId >
9
9
<version >1.0-SNAPSHOT</version >
10
10
11
11
<properties >
You can’t perform that action at this time.
0 commit comments