@@ -16,8 +16,12 @@ public static void main(String[] args) throws IOException {
16
16
//creating a url, the url link denotes a php file that has some string value to be returned to this program when invoked
17
17
URL url = new URL ("http://adhikariaashish.com.np/Java_Network_Connection_demo.php" );
18
18
19
- //connecting to a url
20
- HttpURLConnection httpURLConnection = (HttpURLConnection ) url .openConnection ();
19
+
20
+ //If you call method openConnection() for a URL it returns an URLConnection object.
21
+ URLConnection urlConnection = url .openConnection ();
22
+
23
+ //Since we are using a url whose protocol is HTTP, we need to typecast this connection to HttpURLConnection first before operating
24
+ HttpURLConnection httpURLConnection = (HttpURLConnection ) urlConnection ;
21
25
22
26
//Set the method for the URL request, one of: GET POST HEAD OPTIONS PUT DELETE TRACE are legal
23
27
httpURLConnection .setRequestMethod ("POST" );
@@ -31,6 +35,8 @@ public static void main(String[] args) throws IOException {
31
35
//getOutputStrream() returns the output stream of the URL connection for writing to the resource
32
36
OutputStream outputStream = httpURLConnection .getOutputStream ();
33
37
BufferedWriter bufferedWriter = new BufferedWriter (new OutputStreamWriter (outputStream , "UTF-8" ));
38
+
39
+ //the string "Java_Program_Demo" is checked by the php file to decide whether to return the string value to this java program or not
34
40
String post_data = URLEncoder .encode ("type" , "UTF-8" ) + "=" + URLEncoder .encode ("Java_Program_Demo" , "UTF-8" );
35
41
bufferedWriter .write (post_data );
36
42
bufferedWriter .flush ();
0 commit comments