QNetworkReply: Network access is disabled in QWebView - c++

I cannot load website into my QWebView, QNetworkReply is returning me the error: Network Access is disabled. Loading files from local works.
I am using Qt5. Does anyone know why is connection disabled and how this line affects this situation:
QNetworkProxyFactory::setUseSystemConfiguration(false);
My eth0 connection works properly, and I am able to ping any website.

From the Qt doc : calling setUseSystemConfiguration() overrides any application proxy or proxy factory that was previously set. So be careful to not have set any other proxy before.
Moreover, if you want to check the Network access, you might do it that way :
QNetworkAccessManager m_pManager;
QNetworkConfigurationManager configManager;
m_pManager.setConfiguration(configManager.defaultConfiguration());
connect(&m_pManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
connect(&m_pManager, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)));
and in your slot :
if(accessible != QNetworkAccessManager::Accessible)
{
// case where the network is not available
}
And for the reply, you can check in the slot replyFinished() if there was an error during the process.

Related

DefaultHandshakeHandler's determineUser not called on Production Server

I have a grails project which uses Spring websockets. I have implemented the DefaultHandshakeHandler to create random principal name for each new session and use convertAndSendToUser to send messages.
Everything works fine in local run. I am also able to deploy the WAR file on an AWS EC2 Linux Instance running latest tomcat. The file deploys fine and the Connect and Disconnect events on websockets can be detected correctly.*
The only problem is, My CustomHandshakeHandler's determineUser does not get called on production. Due to this my StompHeaderAccessor's principal is always null and the code starts spitting NPEs.
This is how i have declared the CustomHandshakeHandler :
class CustomHandshakeHandler extends DefaultHandshakeHandler {
#Override
protected Principal determineUser(ServerHttpRequest request,
WebSocketHandler wsHandler,
Map<String, Object> attributes) {
// Generate principal with UUID as name
return new StompPrincipal(UUID.randomUUID().toString())
}
}
This is how i set the handshake handler configuration :
#Override
void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
stompEndpointRegistry.addEndpoint("/ws-ep") // Set websocket endpoint to connect to
.setHandshakeHandler(new CustomHandshakeHandler()) // Set custom handshake handler
.withSockJS() // Add Sock JS support for frontend
}
I tried deploying the same WAR file on local Tomcat 8 and it again works fine. It seems problem happens due to AWS. I also searched for some other AWS and websocket related issues. I came across some ELB compatibility related things but i Don't think that's my case since my websockets are working fine (Events are being received)
Can someone please help out or point me in right directions
Problem was with Nginx settings which upgrade the Http Request to WS request. This question helped a lot : Spring WebSocket: Handshake failed due to invalid Upgrade header: null

Jetty http/2 add ServerSessionListener to server

i am using embedded Jetty to implement HTTP/2. At the moment i am trying to add a ServerSessionListener (from: org.eclipse.jetty.http2.api.Session.Listener.Adapter) to my Server.
i tried to add it to the Context and Server via: addEventListener with no success.
Maybe someone can give me a hint about what i am doing wrong..
I want to add a session Listener to my HTTP2 Connection to track the connected Sessions (Clients) and their connection duration.
Regards!
You can add an implementation of Connection.Listener as a bean to the connector itself, for example:
http2Connector.addBean(new Connection.Listener()
{
public void onOpened(Connection connection) { ... }
public void onClosed(Connection connection) { ... }
});
Alternatively you can add the Connection.Listener as a bean to the ConnectionFactory.
In both cases, every time a connection is created, the listener is added to the connection and will be invoked when the connection opens and when it closes.
You can use Jetty's ConnectorStatistics class that already gathers a number of statistics about connections and already implements Connection.Listener.

How to solve error in windows phone 8 application when retrieving app from background with web services?

I am developing application and facing a problem when I use web services.
At any page which request web service, if app goes to background (by search or menu press for example) before request complete it causes an error when I retrieve the application from background again:
An error (Exception of type 'System.Net.WebException' was thrown.) occurred while transmitting data over the HTTP channel.
Any solution for this problem ?
When your application is sent to background, it is suspended, and all connections are therefore cut off. There's nothing you can do about that. Just catch the error and retry the call to the webservice.
You need to call the webservice again when the application is reactivated. One way of doing it is to maintain a bool variable flag.
set it to true when the webservice is started.
set it back to false when the response is received
in the Application_Activated event handler(App.xaml.cs) check the status of flag and call the webservice again if the flag is true.
Otherwise if you just wish to keep your webservice running under lock screen then set
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
If your app goes to background your webrequest wont complete, You cant do anything about that. But you can prevent crash.
When you know the app is going background ( may be in onNavigatedFrom()) you detach the handler you previously attached to xxCompleted event or handle the thrown exception in handler for xxCompleted in the same way. The first solution implies you service client object must be a class member (private maybe), otherwise it wont be in scope in onNavigatedFrom(). To complete the request later you may use a marker ( bool successful, required) and in OnNavigatedTo() you can do like:
if(required && !successful)
{
// make the request again
}
still exception will be caught in the generated reference file, but the app wont crash and you'd know when you need to make the webrequest again.
I found the same problem, when I tried to fetch a sas uri and calling the PhotoChooserTask.show() simultaniously. So I had to ensure that webrequest is complete before the calling PhotoChooserTask.show().

how to download files with ftp url behind http proxy

I am behind a http/https proxy. So to download a file using QNetworkAccessManager, i set the proxy as following:
if(no_proxy)
{
QNetworkProxyFactory::setUseSystemConfiguration (false);
QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy);
}
else if(system_proxy)
{
QNetworkProxyQuery pQuery(QUrl(QLatin1String("http://www.google.com")));
QList<QNetworkProxy>listOfProxies =QNetworkProxyFactory::systemProxyForQuery(pQuery);
QNetworkProxy::setApplicationProxy(listOfProxies.first());
}
else if(manual_proxy)
{
proxy.setHostName(address);
proxy.setPort(port);
if(http_proxy)
proxy.setType(QNetworkProxy::HttpProxy);
else if(socks_proxy)
proxy.setType(QNetworkProxy::Socks5Proxy);
else if(ftp_proxy)
proxy.setType(QNetworkProxy::FtpCachingProxy);
QNetworkProxy::setApplicationProxy(proxy);
}
Now behind http squid proxy server, this code works fine in case of http urls. But, if i try to download a file with ftp url the download fails with the error
no suitable proxy found
It does not seem to use http proxy for ftp urls. But, we have such options like in firefox:
use this proxy server for all protocols
How to do similar thing in Qt!
Update:
void DownloadThread::startDownload()
{
QString args =downUrl,tempFN;
QUrl url = QUrl::fromEncoded(args.toLocal8Bit());
request.setUrl(url);
request.setRawHeader("User-Agent", userAgent);
request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
reply.setCookieJar(cookieJar);
reply=manager.get(request);
connect(reply, SIGNAL(readyRead()),this,
SLOT(saveToDisk()));
...
}
Have you tried explicitly setting the Qftp proxy?
int setProxy ( const QString & host, quint16 port )
That might get you more joy, but yes, you have to set the proxies up for each connection normally, however, there is always the possibility that the proxy you are trying to use doesn't support FTP? If you pass me some more details about the proxy and where your problems lie (request/response code for example)
Also in squid.conf may want to change/add the following in case they are not present
acl SSL_ports port 443 21
acl FTP proto FTP
always_direct allow FTP
http_access allow ftp
Also, worth checking that the firewall allows port 20, 21 & 443 (I know its a simple check, but often I find its things like these that can be a real pain to find as a root cause).
Do you have a copy of the log file that is generated? it would be interesting/helpful to see what error code is being returned. Also, have you tried manually stepping through the program to see what is contained in the variables at run-time, that would give you a better picture of what is happening, as it may be that everything is fine and that there is a simple way to progress which the contents of the variables will lead you to in short order (might not be the case but it usually worth a try).

How do I set timeout for TIdHTTPProxyServer (not connection timout)

I am using TIdHTTPProxyServer and now I want to terminate connection when it is success to connect to the target HTTP server but receive no response for a long time(i.g. 3 mins)
Currently I find no related property or event about it. And even if the client terminate the connection before the proxy server receive the response from the HTTP server. OnException Event will not be fired until the proxy server receive the response. (That is, if the proxy server still receive no response from HTTP Server, I even do not know the client has already terminate the connection...)
Any help will be appreciated.
Thanks!
Willy
Indy uses infinite timeouts by default. To do what you are asking for, you need to set the ReadTimeout property of the outbound connection to the target server. You can access that connection via the TIdHTTPProxyServerContext.OutboundClient property. Use the OnHTTPBeforeCommand event, which is triggered just before the OutboundClient connects to the target server, eg:
#include "IdTCPClient.hpp"
void __fastcall TForm1::IdHTTPProxyServer1HTTPBeforeCommand(TIdHTTPProxyServerContext *AContext)
{
static_cast<TIdTCPClient*>(AContext->OutboundClient)->ReadTimeout = ...;
}