I am using Jersey client 2.25.1 and the queryParam(...) method does not correctly encode the values, e.g this approach fails:
Response response = client.target(uri)
.queryParam("a", "%20") // Translated into space ' '(!)
.queryParam("b", "{") // Will ruin template parsing(!)
.queryParam("c", "}") // Will ruin template parsing(!)
.queryParam("d", URLEncoder.encode(" ", "UTF-8")) // Using explicit encoding will translate ' ' into '+'(!)
.request().get();
So the workaround suggested is like this:
Response response = client.target(uri)
.queryParam("a", "{a}").resolveTemplate("a", "%20")
.queryParam("b", "{b}").resolveTemplate("b", "{")
.queryParam("c", "{c}").resolveTemplate("c", "}")
.queryParam("d", "{d}").resolveTemplate("d", " ")
.request().get();
This works, but it puzzles me that this very common use case doesn't have better library support from the Jersey client(?) I want to be able to have an arbitrary value properly URL encoded as a query parameter, so that exact same value will reach the server side.
Are there alternatives to this solution? Which is the preferred/best way to solve this?
Related
Using the Kanna import I am currently parsing html using the following code:
if let doc = Kanna.HTML(url: NSURL(string: "https://en.wikipedia.org/wiki/Data")!, encoding: NSUTF8StringEncoding) {
// Search for nodes by XPath
for link in doc.xpath("/html/head...") {
primaryDisplay.text!=link.text!
print(link.text)
}
}
}
I was wondering how to identify specific "nodes"(not sure if that is the correct term) in/on a html page to parse the specific data I want...
Here is a image that shows what it is I wanted to know... I think...
A simple way to do what are you finding is using SwiftSoup
Try this:
do{
let html = "<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<title>Some webpage</title>" +
"</head>" +
"<body>" +
"<p class='normal'>This is the first paragraph.</p>" +
"<p class='special'><b>this is in bold</b></p>" +
"</body>" +
"</html>";
let doc: Document = try SwiftSoup.parse(html)
let els: Elements = try doc.getElementsByClass("special")
let special: Element? = els.first()//get first element
print(try special?.text())//"this is in bold"
print(special?.tagName())//"p"
print(special?.child(0).tag().getName())//"b"
}catch Exception.Error(let type, let message)
{
print("")
}catch{
print("")
}
You should also take a look at xpath/xquery - it is a language specifically intended to traverse and query XML, which makes it applicable to XHTML and well HTML. XHTML is basically well formed HTML.
Assuming you had an xpath/xquery parser installed on your machine, you could...
get a list of all the p elements in the document: //p
get a list of all the p elements having class "special": //p[#class = 'special']
XQuery adds the ability to query documents using a SQL like syntax called FLWOR.
The difficulty in using this or any other parser for html is that often, the HTML is not well formed. That means that every opening tag does not have a closing tag. This makes any kind of parsing somewhat sketchy as the parser may not be able to figure out the hierarchy implied by the HTML.
I am trying to use the late static binding concept during insertion but I am getting a syntax error when I am writing this statement:
I am using php version 5.3.8
$resultArray = $this->connection->query("insert into " static::$table "(title,link) values('hi','hello')");
Looks like you forgot some dots to concatenate static::$table with the rest of the query string. Try this:
$resultArray = $this->connection->query("insert into " . static::$table . "(title,link) values('hi','hello')");
Is there an Android class that (correctly) encodes URLs containing unicode characters? For example:
Blue Öyster Cult
Is converted to the following using java.net.URI:
uri.toString()
(java.lang.String) Blue%20Öyster%20Cult
The Ö character is not encoded. Using URLEncoder:
URLEncoder.encode("Blue Öyster Cult", "UTF-8").toString()
(java.lang.String) Blue+%C3%96yster+Cult
It encodes too much (i.e. spaces become "+" and path separators "/" become %2F). If I click on a link containing unicode characters with the Dolphin web browser it works correctly, so obviously this can be done. But if I try to open an HttpURLConnection using any of the above strings, I get an HTTP 404 Not Found exception.
I ended up hacking together a solution that seems to work for this, but is probably not the most robust:
url = new URL(userSuppliedPath);
String context = url.getProtocol();
String hostname = url.getHost();
String thePath = url.getPath();
int port = url.getPort();
thePath = thePath.replaceAll("(^/|/$)", ""); // removes beginning/end slash
String encodedPath = URLEncoder.encode(thePath, "UTF-8"); // encodes unicode characters
encodedPath = encodedPath.replace("+", "%20"); // change + to %20 (space)
encodedPath = encodedPath.replace("%2F", "/"); // change %2F back to slash
urlString = context + "://" + hostname + ":" + port + "/" + encodedPath;
URLEncoder is designed to be used to encode form content, not whole URI's. Encoding / as %2F is intentional to prevent user input from being interpreted as a directory, and + is valid encoding for form data. (form data == part of the URI following the ?)
Ideally, you would encode "Blue Öyster Cult" before appending it to your base URI, instead of encoding the whole string. And if "Blue Öyster Cult" is part of the path instead of part of the query string, you have to replace + with %20 yourself. With these restrictions, URLEncoder works fine.
Here's the original code:
res.write JSON.stringify {"#{result.statusCode}": "OK"}
and here's the error that both the CoffeeScript linter in SublimeText 2 and the "Try CoffeeScript" interpreter on the CoffeeScript site give me:
PARSE ERROR ON LINE 1: UNEXPECTED '('
Obviously there's no open parens in the code, so I don't understand the error. Is it a bug in the CoffeeScript parser?
The smallest line of code that does this seems to be something like this:
{"#{a}": ""}
I'm assuming that string interpolation in an object's key is valid, but I don't know for sure.
EDIT:
After some investigation it seems that it's not valid to do the string interpolation in the key because the resulting JavaScript would be invalid.
This:
{"#{a}": "stuff}
would translate to something like:
{ "" + a: "stuff"}
which isn't valid.
But can someone explain why the error message it gives me is so wrong?
I'm assuming that string interpolation in an object's key is valid, but I don't know for sure.
Unfortunately it's not.
You'll have to do something like
(json = {})[result.statusCode] = 'OK'
res.write JSON.stringify json
or if you want a one-liner
res.write (-> ((json = {})[result.statusCode] = 'OK') and JSON.stringify json)()
As for the misleading error, CoffeeScript is trying to translate your {"#{a}": ''} into {("" + a): ""} which is not valid JavaScript. CoffeeScript is throwing the error at that left paren.
I'm using a http call to post information about my app in any user's wall. I do it in this way:
_url = "https://graph.facebook.com/" + user_id + "/feed?message=MSG";
_url += "&access_token=" + access_token;
_url += "&picture=" + fb_app_url + "fb_icon.png";
_url += "&name=" + String_ToUrl("MY GAMES");
_url += "&link=" + "http://www.nlkgames.com";
_url += "&description=String_ToUrl("descripción with accent")"
_url += "&method=post";
http.URL_CALL(_url);
This method post right the information in the user's wall, but the accents are shown by a diamond with a question sign inside it. I don't know how to make it works with accents.
String_ToUrl will encode string in this way:
"descripción with accent" = "descripci%E9n+with+accent"
What's my fail? Why the user's facebook's wall doesn't recognize the urlencoded?
Instead of encoding it for URL, what happens if you just post the description as is?
If that doesn't work, try converting the string using HTML entities and then URL Encoding the description.
you could also try to replace those letters with the ASCII representation (somewhat like \u123) - that would be what the Graph API gives you when there is such a Special character (in my case it would be äöü from the german alphabet)
Your String_ToUrl function doesn't encode URL in unicode aware way.
ó (aka 'LATIN SMALL LETTER O WITH ACUTE' (U+00F3)) should became %C3%B3 in URL encoded form, and not %F3 (in your case you indicated that it became %E9 which in fact é character).
descripción -> descripci%C3%B3n
Be sure to use proper (and unicode aware) way to encode your parameters, in JavaScript for example encodeURIComponent should be used...