I am having a curious problem that perhaps someone has insight into. I encode a query string into a URL on Android using the following code:
request = REQUEST_BASE + "?action=loadauthor&author=" + URLEncoder.encode(author, "UTF-8");
I then add a few other parameters to the string and create a URI like this:
uri = new URI(request);
At a certain point, I pull out the query string to make a checksum:
uri.getRawQuery().getBytes();
Then I send it on its way with:
HttpGet get = new HttpGet(uri);
On the Appengine server, I then retrieve the string and try to match the checksum:
String query = req.getQueryString();
Normally, this works fine. However, there are a few characters that seem to get unencoded on the way to the server. For example,
action=loadauthor&author=Charles+Alexander+%28Ohiyesa%29+Eastman×tamp=1343261225838&user=1479845600
shows up in the server logs (and in the GAE app) as:
action=loadauthor&author=Charles+Alexander+(Ohiyesa)+Eastman×tamp=1343261226837&user=1479845600
This only happens to a few characters (like parentheses). Other characters remain encoded all the way through. Does anyone have a thought about what I might be doing wrong? Any feedback is appreciated.
I never did find a solution for this problem. I worked around it by unencoding certain characters on the client before sending things to the server:
request = request.replace("%28", "(");
request = request.replace("%29", ")");
request = request.replace("%27", "'");
If anyone has a better solution, I am sure that I (and others) would be interested!
URLEncoder does not encode parentheses and certain other characters, as they are supposed to be "safe" for most servers. See URLEncoder. You will have to replace these yourself if necessary.
Example:
URI uri = new URI(request.replace("(","%28"));
If a lot of replacements are needed, you can try request.replaceAll(String regularExpression, String replacement). This, of course, requires knowledge of regular expressions.
Related
I'm trying to do a post request with a WebView on Android.
After searching for days and trying dozens of things i couldn't get it work. In SWIFT it's just a few lines of code so i thought there must also be a simple way to do a post request for a webview on android.
As (for 2016) EncodingUtils and HTTPClient are deprecated this are my current approaches:
String url = "http://example.com/php.php";
String postData = null;
postData = "param1=" + URLEncoder.encode("1234567890", "UTF-8");
webcontent.postUrl(url,postData.getBytes());
//or
webcontent.postUrl(url, Base64.encode(postData.getBytes(), Base64.DEFAULT));
Both just result in a blank screen. There is just one parameter to be sent and a string containing html from the server should be received.
In addition, the php on the server returns a html-string with colored background irrespective of any input, but even this isn't displayed so maybe the whole request never reaches the server?
Thanks in advance!
In Android you do not use webView to access the content of the HTTP response. You'll need to use HttpClient for that purpose!
See this nice tutorial which explains the fundamentals! Also see this video if you find it hard!
Hope it helps!
I'm trying to make an HTTPGET request to a REST server, the URL i need to send contains many parameters:
This is the URI :
http://darate.free.fr/rest/api.php?rquest=addUser&&login=samuel&&password=0757bed3d74ccc8fc8e67a13983fc95dca209407&&firstname=samuel&&lastname=barbier
I need to get the Login,password,first, name and last name that the user types, then produce an URI like the once above.
Is there any easy way to create the URI, without concatenate the first part of the URI http://darate.free.fr/rest/api.php?rquest=addUser with every &¶meter:value
I prefer to use Uri.Builder for building Uris. It makes sure everything is escaped properly.
My typical code:
Uri.Builder builder = Uri.parse(BASE_URI).buildUpon();
builder.appendPath(REQUEST_PATH);
builder.builder.appendQueryParameter("param1", value);
Uri builtUri = builder.build();
I hope you can use webview.posturl shown below
webview.postUrl("http://5.39.186.164/SEBC.php?user="+username));
It also worked fine for me to get the username from the database. I hope it will help you.
I'm having a slight problem opening a certain URL in the browser. First of all I use the following code to launch the browser:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Globals.currentChatURL));
startActivity(Intent.createChooser(browserIntent, "Select browser:"));
Now if I set Globals.currentChatURL to something like http://www.google.com then it opens that site just fine. But my URL is a little more complicated as it contains multiple parameters which are all base64 encoded. Here is an example of how my URL looks:
http://webportal.mysite.com/ChatProgram/chat.php? intgroup=UFYyMA==&intid=UFYyMEZN&hg=Pw__&pref=user&en=U0NPVFQgTUlMTEFS&ee=cGF1bGdAbWFnbmF0ZWNoLmNvbQ==&eq=UFRWRkVI&ec=TUFHTkFURUNI
Now if I use my above code to try and launch this URL it brings me to the Google search page with the following message:
"Your search - http://URLabove ... did not match any documents"
Yet if I copy the URL and paste it into the address box it brings me to the right place. How can I fix this?? The whole point of this is to have the user click the button and the site to launch, not for the user to have to copy and paste the URL manually.
Any suggestions would be greatly appreciated.
Thanks a lot
There is unwanted equal signs in the query part of your http URI. Such signs have a specific meaning as delimiters in the form ¶meter=value.
This equal signs represents padding values (0, 1 or 2) from your base64 encoding.
You can either
remove them because your base64 server decoder won't bother reconstructing them, or
percent encode them (with all other reserved characters).
In android you can use percent encode this way:
String value = URLEncoder.encode("annoying values with reserved chars &=#", "utf-8");
String url = "http://stackoverflow.com/search?q=" + value;
The RFC 2396 is now deprecated but that is what URI.parse() is based on as stated by the documentation:
uriString an RFC 2396-compliant, encoded URI
In my app I receive a URL such as
http://www.wassersportlotse.de/php/lib/smart_image_resizer/image.php/Mühlendammschleuse.jpg?image=/media/images/uploads/Mühlendammschleuse.jpg
When there are no German characters in the fullurl I can just use it without encoding and it works fine. However if I receive a URL such as the one above it doesn't work (the ü is causing the problem). Below I have tried to encode the seperate parts of the URI to no avail. As alway advice is very much appreciated.
public ImageDownloader(String fullurl) throws URISyntaxException{
URI uri = new URI(fullurl);
path = uri.getPath();
path = URLEncoder.encode(path);
query = uri.getQuery();
query = URLEncoder.encode(query);
auth = uri.getAuthority();
url = "http://" + auth + path + query;
}
Maybe the encoder das encode the Umlaut as UTF-8 characters (so ü would be encoded with two characters) and they are not put back together properly at the server (for us it didn't work with Tomcat). To solve this situation we used URLEncoder.encode(param, "ISO-8859-1") to encode the parameters.
There's no simple answer, because it depends on the server serving that URI which encoding is expected.
Usually it's UTF-8.
In that case: use String.getBytes, specifying the UTF-8 encoding, and obtain a byte array from that. Re-encode that byte array as string by taking all bytes <= 127 as-is, and substituting all others by the %hh form. (percent sign, then two hex digits). See http://greenbytes.de/tech/webdav/rfc3986.html#rfc.section.2.1.
You can use Android's Uri class to help you out. That class has an encode() method which will use UTF-8 to encode your string.
I recently had a problem with URLs for images whose names included umlauts and German special characters, and I lost a day looking for the solution. The images simply did not appear if there was an ä or and ü in the file name or the directory name. I thought it might be spring, or some other Java technology I am working with, or in the browser. And strangely enough, even with the url encoded, it failed to find the image. But in the end, the solution was in my tomcat server.xml configuration. In your server.xml file, find your connector and add these two lines:
URIEncoding="UTF-8"
useBodyEncodingForURI="true"
At the end, it should look something like this:
<Connector connectionTimeout="20000"
port="8080"
protocol="HTTP/1.1"
redirectPort="8443"
URIEncoding="UTF-8"
useBodyEncodingForURI="true"/>
Now I do not need to url-encode the url. This is a help to my clients, because they can see the German words in the urls spelled correctly.
Here is another tip: if you are coding in eclipse and starting and stopping your server from inside eclipse, then the configuration file (server.xml) could be in your eclipse workspace in the Servers folder. It must be changed here for it to work with eclipse. This can be maddening, when you have made the change in your principal tomcat configuration, and the urls work there, but they are still broken when running the server in eclipse.
That did it for me. I hope it helps someone out there! :-)
Have your tried unsing:
android.net.Uri.encode(urlString, ":/");
It encodes the string but skips ":" and "/".
In my browser, or in iOS, when I try to get the contents of a URL with encoded http authentication information in the form
http://myUser:myPassword#www.example.com/secure/area/index.html
It just works. I'm getting URLs from a web service, and I'd like to avoid trying to parse them up for their HTTP auth info if I can help it. Is there a way to do something similar in Android without actually parsing the URLs? Alternatively, what is the best way to go about that?
UPDATE:
I find that when I try to set the authentication information in an Authorization header, I get a very strange FileNotFoundException.
Here's the code I'm using:
URL url = new URL(urlString);
URLConnection connection;
String authority = url.getAuthority();
if (authority.contains("#")) {
String userPasswordString = authority.split("#")[0];
url = new URL(urlString.replace(userPasswordString + "#", ""));
connection = url.openConnection();
String encoded = new String(Base64.encode(userPasswordString.getBytes(), Base64.DEFAULT), "UTF-8");
connection.setRequestProperty("Authorization", "Basic " + encoded);
} else {
connection = url.openConnection();
}
InputStream responseStream = connection.getInputStream();
All the info seems to check out, I've verified the url is correct, the base64 string is correct, and the file is certainly on the server--I have no trouble at all opening it with Firefox, and Firebug shows all the right headers, matching what I've sent as far as I can tell. What I get though is the following error (url host changed to protect the innocent):
java.io.FileNotFoundException: http://a1b.example.com/grid/uploads/profile/avatar/user1/custom-avatar.jpg
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1061)
Any idea what this is all about?
I looked into using HttpClient, but saw that in Issue 16041 it is recommended that we prefer URLConnection.
That looks like your browser is applying some extra rules to parsing the URL. In Android you can use HTTP Client's authentication mechanism such as BASIC and DIGEST to do the same things. Which one you choose is dependent on the server you are trying to authenticate against.
Here is a good page to get you started.
Unfortunately, on Android you can't pass the user info (username/password) in that format to either java.net.URL or HttpClient and have it work like in a browser.
I'd recommend using URI (see http://download.oracle.com/javase/1.5.0/docs/api/index.html?java/net/URI.html) to do this: pass your URL to the URI constructor that takes a String and then you can extract the user info (using getUserInfo()). You can then either use HttpClient's authorization classes (see http://developer.android.com/reference/org/apache/http/auth/package-summary.html) or build the basic auth header yourself (an example is given at http://www.avajava.com/tutorials/lessons/how-do-i-connect-to-a-url-using-basic-authentication.html).