Uri not Absolute exception getting while calling Restful Webservice - java

The below code snippet is using to call my web service using restful API.
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
String uri= "https://127.0.0.1:8443/cas-server-webapp-3.5.0/login";
WebResource resource = client.resource(URLEncoder.encode(uri));
MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
queryParams.add("username", "suresh");
queryParams.add("password", "suresh");
resource.queryParams(queryParams);
ClientResponse response = resource.type(
"application/x-www-form-urlencoded").get(ClientResponse.class);
String en = response.getEntity(String.class);
System.out.println(en);
And getting this exception while running the above code
com.sun.jersey.api.client.ClientHandlerException: java.lang.IllegalArgumentException: URI is not absolute
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:151)
at com.sun.jersey.api.client.Client.handle(Client.java:648)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
I googled many articles and did'nt get where i am doing wrong .
Side note :cas-server-webapp-3.5.0 war deployed on my machine in Apache tomacat7

An absolute URI specifies a scheme; a URI that is not absolute is said to be relative.
http://docs.oracle.com/javase/8/docs/api/java/net/URI.html
So, perhaps your URLEncoder isn't working as you're expecting (the https bit)?
URLEncoder.encode(uri)

For others who landed in this error and it's not 100% related to the OP question, please check that you are passing the value and it is not null in case of spring-boot: #Value annotation.

The problem is likely that you are calling URLEncoder.encode() on something that already is a URI.

Maybe the problem only in your IDE encoding settings. Try to set UTF-8 everywhere:

In an API Key Authorization Scenario...
You may be performing the 2nd REST call after getting an AUTH_TOKEN and ENDPOINT_URL from the first REST call.
Check your concatenation of "<ENDPOINT_URL> + <API_METHOD_URI>", you may be sending only the API_METHOD_URI.
This happened to me using the Streamsets integration platform trying to connect to Oracle's Responsys API.

For me, I was getting this error, when configuation in yaml files, which composed my URL was changed. oops,

Related

Open non-http links in browser in java

I have an url in my app like "zoommtg://us04.zoom.com". I want to open it in browser by Intent. As it is not http or https, it can't be parsed by Uri.parse(url). Again if I try url="http://"+url; it works but deletes the ":" from "zoommtg://" resulting wrong url! I am using the solution of this Question
if it is working for https but not other requests, then you can try setting cleartexttraffic to true in your AndroidManifest.xml file.
Do tell if that fixes it. :)
zoommtg seems to be "custom protocol" declaration, which isn't resolveable by web browser, but if you are shure, that http(s) url version will work then just replace scheme
String url = "zoommtg://us04.zoom.com";
url = url.replaceFirst("zoommtg", "https")
if you really need to use unsecure http then you have to allow app to do such requests, see how to
btw. you still can use Uri class, it isn't limited to web protocols... check out some description of this structure
URI = scheme:[//authority]path[?query][#fragment]
you can parse your String to Uri and just replace scheme part
Uri.Builder builder = Uri.parse(url).buildUpon();
url = builder.scheme("https").build().toString();

Url encoding issue with Jersey Client

I need to make a service call such as this:
http://myservice.com/path?var1=value1&var2=value2
The issue I have is value1 and value2 ends up getting encoded, and this makes the service call fail. For example, value1 is something like "a=b&b=c;2&&="... it contains special characters, basically.
I am guessing that this is an issue for the service to fix - to properly handle decoding encoded characters, which I do not think it is currently doing.
Here is a sample of how I am making these requests:
WebTarget target = client.target("http://test.com")
.path("path1")
.queryParam("var1", var1);
Builder builder = target.request();
...
What's puzzling to me is that if I make the same request just using Chrome, everything works. So that makes me to believe that I should have some way with the Jersey API of "disabling" the encoding.
Only way I have found so far to use "raw" Url is to use URI.
So call like this
URI uri = URI.create("http://localhost/~Common~0#/edit?vadf&&sfs&&fdsfd=fs&fsd");
WebTarget target = client.target(uri);
You get request url
1 > GET http://localhost/~Common~0#/edit?vadf&&sfs&&fdsfd=fs&fsd
Everything else I tried resulted in encoding special characters.

Sending simple as URL parameter HttpClient GET request

I am developing an Android application in which I am trying to send a simple array as a URL parameter, but it is not working properly. I am using a HTTP client and GET method. I have tried this in the following way:
StringBuilder sb = new StringBuilder();
sb.append(URLEncoder.encode(e.getKey(), "UTF-8")).append('=').append(URLEncoder.encode(e.getValue()+"", "UTF-8"));
where e.getValue() is ArrayList<Integers>
My URL params are appended %5B28%5D when I am sending [28]. If I don't use URL encoder then it goes as [28] But I want to use URL encoder. Am I doing anything wrong?
Your code is fine. this is how URL encoding works.
Seems like there issue in server at the time of decoding.
Debug the server for any possible issue with decoding.
also refer this answer for a better way of sending an array in get request.

Jersey/Grizzly POST fails for large URI

Relevant: this and that
I'm developing a POST webservice (jersey/grizzly, for research purposes only) which should be able to handle large URIs. But if a URI exceeds above 8000 characters I get a 400 Bad Request Exception with no further explanation. I debugged/tracked it down to the grizzly maxHttpHeaderSize attribute. I tried to set this attribute but failed. Here is how I start the server and the request:
GrizzlyServerFactory.createHttpServer(BASE_URI, new PackagesResourceConfig("org.test"));
JSONObject s = new JSONObject(webResource.queryParams(queryParams).post(String.class));
Thank you for your help!
Cheers,
Daniel
The problem is that GrizzlyServerFactory returns already started HttpServer, that's why you can not reconfigure it on the fly.
Here [1] I've created a copy of the GrizzlyServerFactory , which doesn't start HttpServer, so the code like:
HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
httpServer.getListener("grizzly").setMaxHttpHeaderSize(Integer.MAX_VALUE);
// don't forget to start the server explicitly
httpServer.start();
Hope that will help.
[1] https://github.com/oleksiys/samples/blob/master/jersey-grizzly2-ext/src/main/java/com/sun/jersey/api/container/grizzly2/ext/GrizzlyServerFactory.java
Have You tried with shorter URI? Probably Your server is unable to handle so long URI.
You can read more about it here: What is the maximum length of a URL in different browsers?
Why don't You put request data from URL to request body?
You can create the grizzly http server without starting it now
public static HttpServer createHttpServer(
final URI uri,
final ResourceConfig configuration,
final boolean start)
So basically you create it normally passing false as 3rd argument.
When you get the HttpServer object back you can httpServer.getListener("grizzly").setMaxHttpHeaderSize(Integer.MAX_VALUE);
As was said before and don't forget to start the server manually.

Access URL with double slash with HttpClient

I'm doing a request on another server like this:
HttpGet req = new HttpGet("http://example.com//foo");
new DefaultHttpClient().execute(req);
However, HttpClient changes example.com//foo to example.com/foo, so the other server (which is not mine) doesn't understand the request.
How can I fix this?
A double-slash is not a legal in the path section of a URI (see RFC2396, sections 3.2, 3.4). The '//' sequence has a defined meaning in the URI, it denotes the authority component (server).
I realize this does not answer your question but the HttpClient is, in fact, behaving in accordance with the HTTP and URL standards. The server your are reading from is not. This appears to be previously reported (https://issues.apache.org/jira/browse/HTTPCLIENT-727) and discarded by the HttpClient team.
It is an illegal URL in fact.
Did you try passing an URI instead of a String?
Did you try / \ \ / ? Or the URL might be equivalent to /default.asp/, /index.html/, /./, /?/, example.com/foo/ or the like.
Otherwise you will need to hack the sources.
I also wanted to do same thing and Apache Http client don't support that.
I managed to get it done using a Netty. I wrote http client using Netty and with that I was able send request with double slash(//) in the path. I used SnoopClient as sample.

Categories