Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Anomalies when passing a string from Java to PHP

extract string

  • Please log in to reply
2 replies to this topic

#1 TheMZ

TheMZ

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 27 October 2010 - 05:00 AM

Hello,

I have written a program to read a certain text file from the hard-drive, parse it, and send it to a remote PHP page.

Here is the file-reader, pushing a parsed line into a stack of Strings S1, from which I later extract a batch of 10 lines, and send them to a remote php file:

After passing the string to the PHP page, the PHP code acquires the relevant data using $_POST["data"], and echos it. I noticed strange anomalies in the echoed strings.
Strange special characters appeared in the place of the normal ones. Strings were cut in the middle.

Part of the original string sent was:

0%Stoica Alina Alina%Formidon Young%74|


Instead, the server logged:

0%Stoica Alina Alina%Formidon YoungU|


The strange characters lead me to believe there's an encoding problem, but I can't find an explanation for the cut strings...

Could this be a problem on the PHP side? Is there a solution?

Thank you for your time, and sorry for the trouble! :)

Edited by TheMZ, 27 October 2010 - 05:58 AM.

  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 27 October 2010 - 05:46 AM

You are sending URL escapes along with your query (%xx), you must URL encode them or send them in a portable base64 format.

From the docs it should be something such as this:
java.net.URLEncoder.encode(yourdata.toString(), "ISO-8859-1");

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 TheMZ

TheMZ

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 27 October 2010 - 05:56 AM

You are sending URL escapes along with your query (%xx), you must URL encode them or send them in a portable base64 format.

From the docs it should be something such as this:

java.net.URLEncoder.encode(yourdata.toString(), "ISO-8859-1");


The problem was solved. I indeed encoded the string using Base64 as you suggested, and it works perfectly!
Thank you for your time and help! ;)
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download