aspose file tools
The moose likes Java Micro Edition and the fly likes Url encoding in GET request Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Reply Bookmark "Url encoding in GET request" Watch "Url encoding in GET request" New topic
Author

Url encoding in GET request

Marco Battaglia
Greenhorn

Joined: Feb 17, 2004
Posts: 4
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

Joined: Mar 07, 2002
Posts: 1427
You have to encode URLs by hand in MIDP.


Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
Marco Battaglia
Greenhorn

Joined: Feb 17, 2004
Posts: 4
Thanx Yuan, I'm reading your book, bought in Italy.
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
Cool!
Greg Schwartz
Ranch Hand

Joined: May 11, 2003
Posts: 132
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

Joined: Apr 05, 2004
Posts: 1
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

Joined: May 11, 2003
Posts: 132
Thanks for sharing that code Roy. Looks like a solid solution!
Vivek Viswanathan
Ranch Hand

Joined: Mar 03, 2001
Posts: 350
Thanks for the code.


Vivek Viswanathan SCJP 1.2, SCJP 1.6,SCJD,SCEA,SCWCD,IBM-484,IBM-486,IBM-141,Ms.NET C# 70-316,SCMAD, LPIC-I
 
IntelliJ Java IDE
 
subject: Url encoding in GET request
 
Threads others viewed
probelms with response.getOutputStream
sending mail using servlet
how to send email from j2me using Servlets
http connection to servlet...
HTTP connection thread problem to SERVLET on APACHE
developer file tools