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

Is there a way to extract all inner objects from a List to a new List [duplicate]
Using charAt() method on a string and bubble sort [duplicate]
Android how to draw Linear layout corners shape like this
I would like to put the information to add in jpanel jscrollpane when you click each tree node
Annotation processing fails with Dagger 1.2.2, Gradle 2.4, Eclipse 4.5.0 Mars
Spring Boot two databases
File writing issues - file is blank
“javax.crypto.BadPaddingException: pad block corrupted” exception during decryption [duplicate]
Closing a Socket with Streams
Adding numbers and subtracting multiples
Jsoup output - the element printed only one time if it duplicated
Neo4j embedded only stores inside one runtime
scala, IntelliJ: stackoverflow exception
XML Transformation using DOMSource, DOMResult & XSLT
to print a pattern using for loops [closed]
Terminating an AWS Code Deployment with Java jar return value

Categories

HOME
yii2
zeromq
mockito
heroku
tinymce
stock
grep
jsrender
frameworks
baqend
acquia
networkx
vifm
vault
google-cloud-ml
spring-tool-suite
moonmail
percona
dosgi
django-admin
highlight.js
undefined
interop
code-review
emulator
scriptcs
mustache.php
url-scheme
libuv
r-raster
frame
fog
hybridauth
azure-sql-database
gammu
http-digest
vxworks
az-application-insights
sql-server-agent
reportingservices-2005
google-closure
node-gyp
outlook-api
http-redirect
.net-4.6.2
menuitem
network-flow
rails-routing
datastax-startup
np-complete
namecoin
python-cryptography
blacklist
time-and-attendance
android-textview
url-masking
py2app
transmitfile
xml-attribute
bstr
gstreamer-0.10
disque
persist
simplewebrtc
sdf
dlna
php-parse-error
independentsoft
qdialog
sframe
microbenchmark
npapi
sorl-thumbnail
pick
java-metro-framework
imdbpy
spring-io
sabredav
smtp-auth
selected
jquery-knob
chuck
regsvr32
windows-phone-7.1.1
amazon-appstore
adk
subgurim-maps
celltable
floating
xfbml
html-input
icanhaz.js
nhibernate.search
bespin
substrings
zend-tool
simpletest
gacutil

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