2

I am trying to learn request and retrive data from server with http protocol on Java this is the code I found on Oracle>Tutorial>networking (Code is pasted at the bottom of question)

Question 1: in out.write("string=" + stringToReverse);why "string=" isn't encoded? like stringToReverse varable

String stringToReverse = URLEncoder.encode(args[1], "UTF-8");

Question 2: there are two codes below one from oracle code and other from android studio tuts

code in oracle tuts

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

android tuts code

inputStream = urlConnection.getInputStream();    
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
                BufferedReader reader = new BufferedReader(inputStreamReader);

why is Charset.forName("UTF-8") missing in oracle code?


Note: explaining from basics is very much useful :)

import java.io.*;
import java.net.*;

public class Reverse {
public static void main(String[] args) throws Exception {

    if (args.length != 2) {
        System.err.println("Usage:  java Reverse "
            + "http://<location of your servlet/script>"
            + " string_to_reverse");
        System.exit(1);
    }

    String stringToReverse = URLEncoder.encode(args[1], "UTF-8");

    URL url = new URL(args[0]);
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);

    OutputStreamWriter out = new OutputStreamWriter(
                                     connection.getOutputStream());
    out.write("string=" + stringToReverse);
    out.close();

    BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                connection.getInputStream()));
    String decodedString;
    while ((decodedString = in.readLine()) != null) {
        System.out.println(decodedString);
    }
    in.close();
}
}
  • Note that your questions about two different things: URL encoding and character encoding (using the UTF-8 charset). These are two entirely different things. – Jesper Mar 20 '17 at 10:49

2 Answers2

1

Question 1:

There is no need to encode "string=" (as it does not contain any special characters as explained in https://docs.oracle.com/javase/6/docs/api/java/net/URLEncoder.html)

Question 2:

The charset in the following example is not explicitly defined:

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

Therefore defaut charset is used (which may not be UTF-8)

Every instance of the Java virtual machine has a default charset, which may or may not be one of the standard charsets. The default charset is determined during virtual-machine startup and typically depends upon the locale and charset being used by the underlying operating system. (https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html)

Václav Struhár
  • 1,739
  • 13
  • 21
  • Hello sir, this is my understanding from your answer . Question1 if the user gives any special character Neither the client side programming nor the server side program should interpret that has special meaning ,rather they should be treated without any special meaning. in order to that we encode them. Please let me know if iam correct.. – Shiva prasad Mar 20 '17 at 15:35
0

In a url the string after ? is called as query string

example.com/users/profile?key1=value1&key2=value2

So for the above url the query string is "key1=value1&key2=value2" In a query string there are key,value pairs which a server script can access.These key value pairs are called as request parameters and are separated by an &.So ?,& ,space etc are called special characters in a url as they are treated specially by the browser.

So what happens in case the value1 itself contains an & character.The server will in advertently end the value1 before & character at user1.

name=user1&23=hello&place=hyd

If you see above example it will not work as expected. So that's why you use url encoding to convert special characters like & ,? , space etc to some other non special characters when they are used in query string.The server will convert back them to their actual form once it is received.

Now coming to your question 1),URL encoding is not needed in your case as you are not sending the string_to_reverse as a request parameter in query string.As jesper pointed out this is not url encoding.You are sending it as body using the outputstream.

Now question 2),If you see the http://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html class,it states as below

Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format.

So html form data is posted as application/x-www-form-urlencoded and in ur case URLEncoder is taking care of that.If no charset is specified the default character set is used.How to Find the Default Charset/Encoding in Java?.

The name URL in URLEncoder class is little misleading to you as its not really used for encoding url here but used for encoding the request body(string_to_reverse)as application/x-www-form-urlencoded.

Community
  • 1
  • 1
crackerplace
  • 5,305
  • 8
  • 34
  • 42