java - Get the final URL of a redirected request with unconventional scheme -
my code (below) tries final url returned server bit of redirecting. works fine long urls have http scheme. problem arises when want homecoming url different scheme. want, in situations, homecoming market:// url or other app launch schemes since android , want start intents them.
so gets me far retrieving final http url, when final url market:// throws exception seen (java.lang.illegalstateexception: scheme 'market' not registered), , geturi doesn't provide one, it'll provide whatever before that.
defaulthttpclient client = new defaulthttpclient(); httpcontext httpcontext = new basichttpcontext(); httpget httpget = new httpget(minitialurl); seek { client.execute(httpget, httpcontext); } grab (illegalstateexception e) { e.printstacktrace(); } // parse out final uri. httphost currenthost = (httphost) httpcontext.getattribute(executioncontext.http_target_host); httpurirequest req = (httpurirequest) httpcontext.getattribute(executioncontext.http_request); homecoming (req.geturi().isabsolute()) ? req.geturi().tostring() : (currenthost.touri() + req.geturi());
now, register market:// scheme, don't want hard-code beforehand valid schemes are, want take them , homecoming them whatever are.
any ideas? maybe i'm not taking right approach. (changing server behavior isn't alternative in case... i've got deal redirects.)
my hope can tell me how httpclient ignore scheme, or @ to the lowest degree preserve final uri tries access.
using httpurlconnection
job works me. next of redirects stops without exception when target resource not http resource.
httpurlconnection connection = (httpurlconnection) new url(minitialurl).openconnection(); connection.setinstancefollowredirects(true); string location = connection.getheaderfield("location");
java http apache-commons-httpclient
No comments:
Post a Comment