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

Popup message comming on mac asking for allow incomming connections on every launch of application
Setting Thread's name in constructor
How to format the date from the below types
Radio Buttons into dynamic Word document
Not able to sign up in firebase with android using email\password authentication
API in Spring Boot Repository vs DAO
Java RS null pointer exception when switching from Intellij to Maven build
How to change the image of an ImageButton when the user click the button
How can I make a Text View change it's position based on what the user puts in the Text View above?
How can I use external libraries with the scripts atom package and java?
Compare time offset Using Two Date objects
Chain Matrix Multiplication : Multiply Algorithm not working
JPanel takes the entire space of JFrame
How do I open a Fragment on dialog click?
Android listview in API integration JSON parsing
Create URL based on user input

Categories

HOME
client
image
comparison
xmpp
spring-jdbc
electronics
c#-2.0
upload
esper
windows-10-universal
imacros
correlation
ups
worldwind
flux
dbext
nas
tar
mmap
restful-authentication
vlsi
dynamic-featured-image
user-interaction
internet-explorer-9
fluentvalidation
ejabberd-module
large-file-upload
gitignore
nat
instant-messaging
bcd
twilio-api
column-family
pim
lxd
openoffice.org
disassembling
qwerty
x11-forwarding
hot-module-replacement
sqlite2
leading-zero
squib
turbogears
modelmapper
gpx
flickr-api
apache-fop
dds
logparser
winscp-net
mplayer
csound
worker-thread
diagnostics
knockout-components
feeds
typed-lambda-calculus
font-size
account-kit
medium.com
android-fonts
fody
trash
autorest
sigabrt
sdf
php-parse-error
photobucket
historian
freedesktop.org
appfabric-cache
processmodel
unity5.2.3
offloading
java-metro-framework
preferences
rdl
castle
openexr
fluentautomation
jubula
concurrent-collections
message-driven-bean
java.nio.file
fmod
geos
runtime.exec
jplaton
batterylevel
random-seed
http-unit
typoscript2
jquery-knob
coderush
gdata-api
twrequest
wse3.0
subscript
nsdatecomponents
mercurial-server
celltable
oncheckedchanged
w3wp.exe
mirah
firefox-5
pascal-fc
gacutil
genealogy
msdev
geneva-server
caching-application-block
document-conversion

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