Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a custom header along with qtwebkit request

I'm doing some work with PyQt4 and QtWebKit, and in the web page request need to send a custom "Host" header along with the standard HTTP request. I'm not seeing any options for adding custom headers to the request, but this is all new to me so I hope I'm missing something. I'm looking here:

http://doc.qt.digia.com/4.6/qwebsettings.html

Any advice would be greatly appreciated.

like image 634
lennysan Avatar asked Sep 07 '10 22:09

lennysan


People also ask

Can I add custom header to HTTP request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

What is custom header in API?

Custom Headers allow us to add extra content to our HTTP requests and responses, which we can pass between the client and server. We can use custom headers for metadata, such as defining the current version of the API that is being used.


2 Answers

You can set headers on the QNetworkRequest that is sent:

QNetworkRequest request;
request.setUrl(QUrl("http://qt.nokia.com"));
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");

To use that custom request when loading a page, use the overloaded load function:

myWebView->load(request);
like image 60
Kaleb Pederson Avatar answered Sep 30 '22 15:09

Kaleb Pederson


If you want to apply this to all requests QtWebKit makes, you can subclass QNetworkAccessManager and reimplement its createRequest() function to modify headers accordingly.

like image 44
kralyk Avatar answered Sep 30 '22 15:09

kralyk