jetty ,websockets and UTF8 encoding - java

I'm having a little problem. I'm building a small server in java, based on jetty websockets implementations.
The clients are the browsers and I send information using the websockets javascript api.
Everything works great until I send those special characters such as : ă Ț î ș ê ñ ü
So here is the problem. Client 1 sends a message to the server with one of this characters. Server prints the message and then send the message to client 2.
Client 2 receives the message and prints the message on a browser html page and works great The characters are showed correctly.
The problem is when I wanna print the String on the server site. Instead of ă is shows me the ? char. This is causing me problems because I want to insert the text in a database(mysql- with ut8 encoding enabled)
So.. what seems to be problem. The text that is send from the browser is not UT8 encoded? or the jetty websocket implementation is not receiving String in utf8 encoding??
Thanks

Here's a function I use to HTML-encode all special characters in a string (but not html itself (like < or >)). If you apply it before sending a string to the server, everybody should see the same and you can store it in a database table:
function toHtmlEncoded(string){
return string.replace(/[\u0080-\uC350]/g,
function(a) {return '&#'+a.charCodeAt(0)+';';}
);
}

First read this http://kunststube.net/encoding/
Then check everywhere you've converted bytes into Strings (or the reverse). Common places to make a mistake include calling getBytes() on a String without specifying an encoding. Other pitfalls include not setting the encoding in the database connection string.

Related

Keeps getting gibberish in client side while trying to get value in hebrew

I made a java server which the client requests info and gets response from the server. I am using BufferedWriter in order to send info to the client, and the info is in hebrew. The problem is that the client just gets gibberish if the server sends hebrew to him. I already tried different encoding, like ISO-8859-8, UTF-8, Unicode and so on, but none of them helped. I made a check and in the server side, and the string in the server is fine, in hebrew and I also made it so save it to file in hebrew and it worked. I really don't know what to do.. I tried almost every solution here and nothing worked.
Don't use the BufferedWriter for it, I just tinkered a little bit and found out that if you want to write something with UTF-8 encoding (e.g some non-Latin characters) you could just use the DataOutputStream variable, which has the method writeUTF().
It goes like this:
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(someString);
out.flush();

URLConnection.guessContentTypeFromName Errors (Java)

I am currently writing a function to upload files into server.
My filename format is typical like this ACTION#USERNAME.TXT, I run into the error
java.lang.StringIndexOutOfBoundsException: length=41; regionStart=38; regionLength=-28
when on this line of code
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
I did some debugging and realised the problem was caused by the character '#'. Taking it out allows the file to be uploaded. The actual file allows for the # character so I don't really understand why would # cause a StringIndexOutOfBoundsException.
Could anyone enlighten me please?
The # sign has special meaning in a URL.
The #fragment part of a URL (see syntax) is never sent from the client to the server. It is a part of the URL that is handled by the client. It can be sent from the server to the client in links and redirects.
To include a # sign (or any other special character) in the file name, it must be escaped/encoded using %NN hex codes.
The encoding of # is %23, but you really should use a URL encoder.

Encoding Turkish char on Http Post method with Java

I have a problem Turkish Character encoding
I send a xml document with web services on http post methods but When I encoding Turkish Character(Ğ,Ş ı...) asci code java translate &#230 etc.
this time url conneciton is cut data's other partial because & is mean new attribute
so how to solve this problem what can I do before send on java???
It seems you are sending the XML as part of the URL? In that case you'll need to percent-encode it (see RFC 3986)

Java URL Encoding additional characters

I'm having some troubles getting an HTTP Get call to work.
I concatenate the string and print it before opening the connection.
So my string is as the following:
http://example.com?Adri%E1n%20
However, the server is receiving it as:
http://example.com?Adri%EF%BF%BDn%20
I don't know if the problem is on the server side, or when making the call from Java.
Please help.
Additional info: (%E1 = á)
try
System.out.println(URLEncoder.encode("á", "UTF-8"));
prints
%C3%A1

Safe Data serialization for Plain HTTP GET & POST communication

I'm using the client's browser to submit HTTP request.
For report generation the securityToken is submitted as POST, for report download the same token needs to be submitted by the user browser, this time using GET.
What encoding would you recommend for the securityToken which actually represents encrypted data.
I've tried BASE64 but this fails because the standard can include the "+" character which gets translated in HTTP GET to ' ' (blank space).
Then I tried URL Encoding, but this fails because for HTTP POST stuff such as %3d are transmitted without translation but when browser does HTTP GET with the data %3d is converted to '='.
What encoding would you recommend, to allow safe transmission over HTTP POST & GET without data being misinterpreted.
The environment is Java, Tomcat.
Thank you,
Maxim.
Hex string.
Apache commons-codec has a Hex class that provides this functionality.
It will look like this:
http://youraddress.com/context/servlet?param=ac7432be432b21
Well, you can keep the Base64 and use this solution:
Code for decoding/encoding a modified base64 URL

Categories

Resources