在Web服务的Http Post调用中没有得到正确的响应

时间:2012-03-09 11:42:44

标签: qt symfony1

我使用Http Post方法调用Web服务并在响应中获取下面的文本

HTTP / 1061.1061 1061(我在调试中打印了QHttpResponseHeader)

但实际上它应该返回错误代码或xml。所以请在下面的代码中告诉我我做错了什么

//.cpp file code 
 MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
  qDebug() << "start";
  http = new QHttp(this); // http declared as a member of MainWindow class
  connect(http, SIGNAL(requestFinished(int,bool)), SLOT(replyFinished(int, bool)));

  QByteArray data;//(QString("--" + boundary + "\r\n").toAscii());
  data += "Content-Disposition: form-data; name=\"action\"\r\n\r\n";

  data += "Content-Type: text/xml;charset=\"utf-8\"\r\n\r\n";

  data += QString("<LoginData><IMEI>test123</IMEI><email>a@a.com</email></LoginData>").toAscii();
  data += "\r\n";

  data += "Content-Disposition: form-data; name=\"description\"\r\n\r\n";

  data += "\r\n";

  QHttpRequestHeader header("Content-Type","application/soap+xml;charset=UTF-8");
  header.setValue("Host", "http://xxx.com/restwebservice/ForgotPassword.ashx?");
  header.setValue("Accept-Language", "en-us,en;q=0.5");
  header.setValue("Keep-Alive", "300");
  header.setValue("Connection", "keep-alive");
  header.setValue("Referer", "http://xxx.com/restwebservice/ForgotPassword.ashx?");

  qDebug() << "the sent data is :" + data;
  http->setHost("http://xxx.com/restwebservice/ForgotPassword.ashx?");
  http->request(header, data);

  ui->setupUi(this);
}

广告位方法

void MainWindow :: replyFinished(int , bool)
{
    QHttpResponseHeader responce = http->lastResponse();
    qDebug()<<"reply is :" + responce.toString();
}

1 个答案:

答案 0 :(得分:0)

不推荐使用

QHttp,只是为了向后兼容而仍然在这里。

对于新代码,您应该使用QNetworkAccessManager / QNetworkRequest / QnetworkReplyhttp://qt-project.org/doc/qt-4.8/QNetworkAccessManager.html#details