https://twitter.com/sessions?authenticity_token=b1b43178e09c1e6ccec1b3183f1f139f39643aaf&session%5Busername_or_email%5D=ddddd&session%5Bpassword%5D=rrrrr&q=&site_action=https%3A%2F%2Ftwitter.com%2Fsessions&serialized_string=Hello
why is this url throwing a java.net.MalformedURLException??
The only reason I can think of is if your code is mistakenly applying the URL decoder to the URL string before trying to parse the URL. That would replace the %xx escapes with the characters they stand for, and you'd end up with illegal characters in the "query" part of the URL.
Please post the code that throws the exception.
Related
I have a request as follows:
localhost:8000/location/:01
My code takes as input an HttpContext request.
func(HttpExchange r) {
String area_path = r.getRequestURI(); // Equals string "/location/"
}
How do I parse an HttpExchange correctly so I can pull out the "01" from this path and store it as a variable?
That (localhost:8000/location/:01) is not a valid URL or URI
A plain colon character is not legal in the path of a URL or URI. If you want to put a colon in the path, it must be percent-encoded. Furthermore, if this was a URL, it would start with a protocol; e.g. http:.
Now ... it is unclear what the HTTP stack you are using will do with a syntactically incorrect URL / URI, but it could simply be ignoring the colon and the characters after it.
Your code looks a bit odd too. You have tagged the question as [java]. But the code looks like JavaScript rather than Java; i.e. func is a Javascript keyword. But it also looks like you are using the (deprecated) com.sun.net.httpserver.HttpExchange Java class. I don't know what to make of that ...
My advice:
Don't use a colon character in the URL path.
If you must do it, then percent-encode the colon it.
If you cannot encode it properly, then you may need to find and use a different framework for your HTTP request handling. One that will accept and handle a malformed URL / URI in the way that you want. (Good luck finding one!)
Unfortunately, the details in your question are too sketchy to give more detailed advice.
I'm trying to pass some XML into a URL for a service I'm calling, but when I run this, it's giving me an IllegalArgumentException
response = Unirest.post(appSettings.getURL() + "&service=test&xml=<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><test><cid>blah</cid><pw>blah</pw></test>")
Here's the error:
java.lang.IllegalArgumentException: Illegal character in query at index 108: http://test&service=test&xml=%3C?xml version="1.0" encoding="UTF-8" standalone="no"?><test><cid>blah</cid><pw>blah</pw></test>
I think it has something to do with how the XML is getting read in, but I'm having trouble figuring out exactly what.
Spaces are escaped in the URL as %20 and other non alphanumeric characters can be problematic. Try to use UrlEncoder http://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html to encode URL parameters:
Unirest.post(appSettings.getURL() + "&service=test&xml=" + URLEncoder.encode("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><test><cid>blah</cid><pw>blah</pw></test>", "UTF-8"))
You can also try URIBuilder https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URIBuilder.html or other methods Java URL encoding of query string parameters
The XML has slash characters and question mark which are natural part of the URL parameter syntax, but supposedly that's not a problem. Since we are talking about REST, couldn't you pass the XML information along as part of the JSON payload for the request (or response)?
For generic (even binary) URL parameters one hack I can image is to Base64 encode the payload you want to pass (in this case XML), put that in the URL, and on the other end you can Base64 decode it.
Also keep in mind that for security reasons (to block possible web related exploits which often manipulate with huge URLs) browsers, HTTP servers, and frameworks enforce maximum length for the URL. It's in the ballpark of 1-2 kilobytes, so you don't have too much space for XML data.
The request parameter is like decrypt?param=5FHjiSJ6NOTmi7/+2tnnkQ==.
In the servlet, when I try to print the parameter by String param = request.getParameter("param"); I get 5FHjiSJ6NOTmi7/ 2tnnkQ==. It turns the character + into a space. How can I keep the orginal paramter or how can I properly handle the character +.
Besides, what else characters should I handle?
You have two choices
URL encode the parameter
If you have control over the generation of the URL you should choose this. If not...
Manually retrieve the parameter
If you can't change how the URL is generated (above) then you can manually retrieve the raw URL. Certain methods decode parameters for you. getParameter is one of them. On the other hand, getQueryString does not decode the String. If you have only a few parameters it shouldn't be difficult to parse the value yourself.
request.getQueryString();
//?param=5FHjiSJ6NOTmi7/+2tnnkQ==
If you want to use the '+' character in a URL you need to encode it when it is generated. For '+' the correct encoding is %2b
Use URLEncoder,URLDecoder's static methods for encoding and decoding URLs.
For example : -
Encode the URL param using
URLEncoder.encode(url,"UTF-8")
Back in the server side , decode this parameter using
URLDecoder.decode(url,"UTF-8")
decode method returns a String type of the decoded URL.
Allthough the question is some years old, I'd like to write down how I fixed the problem in my case: the download link to a file is created in a GWT page where
com.google.gwt.http.client.URL.encode(finalurl)
is used to encode the URL.
The problem was that the "+" sign a customer of us had in the filename wasn't encoded/escaped. So I had to remove the URL.encode(finalurl) and encode each parameter in the url with
URL.encodePathSegment(fileName)
I know my question is bound to GWT but it seems, URLEncoder.encode(string, encoding) should be applied to the parameter only aswell.
I'm getting an exception saying Java URI Syntax Exception "java.io.IOException: java.net.URISyntaxException: Invalid % sequence: %wl in query at index 88:" when i try to connect from my android application.
It seems to be throwing the exception where in the URL it says "%wl" and following is the URL. is there a work around for this.
http://192.168.111.111:9000/RB/db.svc/upd?LinkId=184617ED1F21&IPs=fe80::1a46:17ff:feed:1f21%wlan0,192.168.1.127,&MNo=0771111111&sPin=000&Status=0
If you want to use % in your URL the first you need to do is to encode it.
So first you need to replace that % with %25 in your string ....1f21%wlan0... with .....1f21%25wlan0.... before connecting.
You can use the following code for encoding the URL in Java
String encodedUrl = java.net.URLEncoder.encode(<your_url>,"UTF-8");
Have a look at the below links for more information.
1.How to encode url in java
2.URL encoding character reference
UPDATE :
If you don't want to use URL encoder then you can try this out :
yourURL.replaceAll("%", "%25");
It is fine here to replace a single special character, but it would be a tedious task to do like this if you have many special characters that require proper URL encoding.
I need a regex pattern that will find and replace brackets in urls to its urls encoding.
For example a base url like:
http://www.mysite.com/bla/blabla/abc[1].txt
will be turned to:
http://www.mysite.com/bla/blabla/abc%5B1%5D.txt
can anyone help please?
EDIT1:
i originaly use commons-httpclient to access this kind of urls.
when I use the first URL I get an "escaped absolute path no valid" exception.
I can't use URLENCODER because when I use it, I get a "host parameter is null" exception.
The following line should do the trick
String s = URLEncoder.encode("http://www.mysite.com/bla/blabla/abc[1].txt", "UTF-8");
Have you tried URLEncoder.encode?
in the java.net.URLEncoder package.
EDIT:
Ok i see... you cannot pass an entire URL to URLEncoder. URLEncoder is mostly used to encode query parameters.
try this instead:
URI uri = new URI("http", "www.mysite.com", "/bla/blabla/abc[1].txt",null);
System.out.println(uri.toASCIIString());