• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Url encoding in GET request

 
Marco Battaglia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my code, sending a request to an http server, I've to add some params/values via GET method, but I noticed that all will works only by formating my http request (for example replacing 'space' with '%20'). My question: Exists an UrlEncoding method? Have I to define any HttpConnection's property.
Thanx in advance.
Marco
HttpConnection http = null;
InputStream iStr = null;
String str = null;
boolean ret = false;
if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
}
if (c == cmdTest) {
if (txt.getString() != null)
System.out.print(txt.getString());
try {
String formatQuery= txt.getString();
http = (HttpConnection) Connector.open(
"http://XXXXXXX/J2meServer/nonQuery.aspx?sql=" +
formatQuery(txt.getString()));
iStr = http.openInputStream();
String msg=new String("");
msg=processServerResponse(http, iStr);
private String formatQuery (String oldQuery)
{
String newQuery=new String(oldQuery.trim());
while (newQuery.indexOf(' ')!=-1)
{
int i=newQuery.indexOf(' ');
System.out.print("\n"+newQuery);
newQuery=newQuery.substring(0,i)+"%20"+newQuery.substring(i+1, newQuery.length());
}
return newQuery;
}
 
Michael Yuan
author
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to encode URLs by hand in MIDP.
 
Marco Battaglia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Yuan, I'm reading your book, bought in Italy.
 
Michael Yuan
author
Ranch Hand
Posts: 1427
 
Greg Schwartz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know of a custom-made class that handles this? I was hoping there was something that already existed for the URL encoding of GET params. If someone has implemented a good system for this and wouldn't mind sharing their code, let me know.
Cheers,
Greg
 
Roy Smith
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private String urlEncoder(String s) {
try {
if (s == null) {
return (s);
}
StringBuffer sb = new StringBuffer(100);
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if ( (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z')) {
sb.append(c);
continue;
}
if (c > 15) { // is it a non-control char, ie. >x0F so 2 chars
sb.append("%" + Integer.toHexString( (int) c)); // just add % and the string
} else {
sb.append("%0" + Integer.toHexString( (int) c)) ; // otherwise need to add a leading 0
}
}
}
return (sb.toString());
} catch (Exception ex) {
System.out.println("Exception, URLencode string is " + s);
return (null);
}
}
 
Greg Schwartz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for sharing that code Roy. Looks like a solid solution!
 
Vivek Viswanathan
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic