0

I'm coding an application to get information of a device on which I have to send a PUT request like this:

connect(&netman,SIGNAL(finished(QNetworkReply*)), this,
    SLOT(reqFinished(QNetworkReply*)));
QByteArray data("0ABF0A25");
QNetworkRequest req(QUrl("http://192.168.1.100:8088"));
req.setHeader(QNetworkRequest::ContentLengthHeader,data.length());
QNetworkReply rep =netman.put(req,data);
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this,
    SLOT(errorSlot(QNetworkReply::NetworkError))):

I know the device is working because it starts its process and if I put a sniffer between my PC and the device and I see the response .

485454502F312E3020323030204F4B200D0A or in plain text 'HTTP/1.0 200 OK \r\n'

but when the slotRequestFinished(QNetworkReply* rep) slot is executed I get no data, no headers and no attributes, and error code 2 (connection closed).

If i execute:

QVariant attr = rep->attribute(QNetworkRequest::HttpStatusCodeAttribute);

I get and invalid variant object, same for the headers.

How can I get the raw packets of the response? That would be handy for this case.

I also noticed on my sniffer that the connection repeats three times, and sends the request all these times for one only put request; Could that be an issue with the library?

4

1 回答 1

0

我必须对 QNetworkAccessManager 类进行子类化以使用 QTCpSocket 实现我的自定义 put 方法以获得适当的响应,我无法创建 QNetworkReply 并将值设置为属性,因此应用了一些解决方法:P 当然还有 readyRead 信号QTcpSocket的连接到这个类的socketRead槽,错误信号也连接到其他槽

。H

#include <QNetworkAccessManager>
#include <QTcpSocket>

class MyNetworkAccessManager : public QNetworkAccessManager
{
    Q_OBJECT
public:
    explicit MyNetworkAccessManager(QObject *parent = 0);   
    virtual void myPut(const QNetworkRequest &request, const QByteArray &data);

signals:
    void dataReceived(QNetworkReply*,const QByteArray data);

public slots:
    void socketRead();

private:
    QTcpSocket socket;
};

.cpp

void MyNetworkAccessManager::MyNetworkAccessManager(const QNetworkRequest &request, const QByteArray &data)
{
    QUrl url = request.url();
    QString host = url.host();
    int len = data.length();
    int port = url.port();
    QString path = url.path();

    QByteArray reqba = QString("PUT %1 HTTP/1.1\r\n\
Content-Type: application/xml\r\n\
Content-Length: %2\r\n\
User-Agent: MyFooAgent/0.1\r\n\
Host: %3:%4\r\n\r\n").arg(path).arg(len).arg(host).arg(port).toUtf8();


    socket.connectToHost(host, port, QIODevice::ReadWrite);
    socket.waitForConnected();  
    socket.write(reqba + data);

}

void MyNetworkAccessManager::socketRead()
{
    QByteArray data = socket.readAll();
    socket.close();
    int code = 0;
    if(data.contains("200 OK"))
        code = 200;
    else if(data.contains("404 Not Found"))
        code = 404;
    //else if ..... foo
    emit dataReceived(data,code);
}
于 2013-10-02T14:28:33.083 回答