4

I'm trying to write a simple application that will launch a browser and send it to a URL based on a user's input.

QDesktopServices::openUrl(QUrl(url));

However, I'd like to pass variables along with whatever URL they submit using POST. For GET, all I'd need to do is simply embed the values into the URL string, but how would I go about adding POST variables?.

Thanks.

László Papp
  • 51,870
  • 39
  • 111
  • 135
William Northern
  • 403
  • 2
  • 5
  • 12

3 Answers3

3

QDesktopServices wasn't designed for this, I'd suggest doing your HTTP POST using QNetworkAccessManager::post instead.

You can then possibly take some information from the HTTP response to open the desktop browser if this is necessary.

jturcotte
  • 1,273
  • 9
  • 9
1

From the official documentation:

bool QDesktopServices::openUrl(const QUrl & url) [static]

Opens the given url in the appropriate Web browser for the user's desktop environment, and returns true if successful; otherwise returns false.

If the URL is a reference to a local file (i.e., the URL scheme is "file") then it will be opened with a suitable application instead of a Web browser.

The short answer is that it was not meant to be a network managet. For that purpose, one could already use the QNetworkAccessManager. It was just a convenient way to add support for opening up an URL as that would require quite a bit of work otherwise. There were no further plans to it to replicate QtNetwork more closely.

Thereby, I would suggest to use something like this to achieve working with post methods given your url:

QUrlQuery urlQuery;
urlQuery.addQueryItem("param1", "value1");
urlQuery.addQueryItem("param2", "value2");
QUrl url = QUrl("http://foo.com");
QNetworkRequest networkRequest(url);
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
networkManager->post(networkRequest, urlQuery.toString(QUrl::FullyEncoded).toUtf8());
Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • 1
    The problem with this solution as far as I understand it is it's just a request to server. I need to open a browser that will automatically log in into a user's area using credentials that the user supplied, and opening a browser. Not just get the response back to the application. It's basically a launcher. Isn't there any way to open a browser as a process and supply the variables like that? Do browsers even accept POST variables as command line arguments? – William Northern Oct 25 '14 at 05:53
  • @WilliamNorthern: post is never a launcher, full stop. Why do you think it is? In addition, why did you accept the other answer then that just writes the same without the nitty-gritty details if that is not sufficient for you? – László Papp Oct 25 '14 at 05:54
0

If you have no issue with maintaining an external web service, you could set up a GET-to-POST redirection service (since QDesktopService::openUrl can pass url query strings to browsers without issue). Two things to keep in mind when going this route are to a) properly validate the requests the service recieves against some sort of whitelist to avoid security issues that stem from open http redirection, and b) to consider URL length limitations of both the user's desktop browser and server handling the redirects.

If we ignore IE and edge, desktop web browsers seem capable of handling URLs 32k-bytes long or better (figure obtained from a quick web search, may be inaccurate). If you're also targeting older android phones, the length limit drops to 8k.

Another way is to use QWebView which doesn't suffer from the same flaws as QDesktopServices: https://doc.qt.io/archives/qt-5.5/qwebview.html#load-1 . The only issue with this is that it will require use of the webkitwidgets module which may or may not be an issue for you.

Side note: I'm also still trying to find a way deal with the QDesktopServices problem. If you found a better way to send a POST request through the user's default browser, please post it here so that others can benifit.

Cheers.

Tenders McChiken
  • 1,216
  • 13
  • 21