java


why Encoding in http request?


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();
}
}
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)
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.

Related Links

Trouble With Generics and Static Inner Classes
How to specify html page globally in Jboss?
splash screen picture doesn’t appear [closed]
Empty JDateChooser
ListView disappears when DatePicker is added
best file to use for configuration with a multiline support
Java EWS API - Determine Original vs Reply vs Forwarded emails
removeAll method with different object types
Ridiculous use of a switch statement
Regarding volatile variable usage
Spring MVC forwarding to controller with different HTTP method
Porting JUnit tests from 3.x to 4.x
How to print out a DOCX file in java?
recording user selections and altering textview strings via java code
Android App - How do I add Nexus 7 Screen Support without having to explicitly define others
How to configure Spring's RestTemplate to return null when HTTP status of 404 is returned

Categories

HOME
atom-editor
angular-material
react-virtualized
syntax
electron
bpmn
kalman-filter
sqlite3
blueprintjs
podio
facebook-php-sdk
nstableview
alignment
phaser
quickfix
jsprit
vaadin7
uitypeeditor
workload-scheduler
finite-automata
serilog
apache-metamodel
dbext
zurb-foundation-6
icloud-api
linkerd
opentype
subdomains
kendo-datasource
gsmcomm
ecto
preg-match-all
code-contracts
vsts-build-task
mmenu
disassembling
space-complexity
opshub
trim
mesos-chronos
janrain
isbn
password-encryption
turbogears
outlook-api
flashair
gpx
qcombobox
alphabet
colorama
crypt
arrow-keys
bind9
orthogonal
minimization
yt-project
ableton-live
wdf
heidisql
removechild
vmware-tools
angstrom-linux
flash-cs5
prettytensor
sigabrt
captivenetwork
uid
jfugue
ora-00900
mutation-observers
spidermonkey
security-testing
emailrelay
method-parameters
bigbluebutton
tablelayout
asp.net-web-api-odata
googlemock
php-5.4
heisenbug
cloud-connect
industrial
farseer
odata4j
gridcontrol
ocunit
angularjs-controller
dmoz
isnullorempty
dice
hosts-file
zpt
yui-datatable
propagation
gamequery
mozilla-prism
user-friendly
exchange-server-2003
backcolor
thread-local-storage

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App