谁能给我一个关于 QHttp post 的例子
我自己写了一个,get没有问题 就是post数据时 不能返回数据,但是服务端看到了post的请求
#include <QtNetwork>
#include <iostream>
#include <QUrl>
#include "HttpCon.h"
#include <QDateTime>
#include <QByteArray>
#include <QTextStream>
#include <QRegExp>
HttpCon::HttpCon(QObject *parent) : QObject(parent) {
http = new QHttp(this);
connect(http, SIGNAL( requestFinished (int, bool )), this, SLOT(soltrequestFinished(int, bool )));
//connect(http, SIGNAL( done(bool )), this, SLOT(done(bool)));
//connect(http, SIGNAL( responseHeaderReceived ( const QHttpResponseHeader )), this, SLOT(mainresponseHeaderReceived(const QHttpResponseHeader)));
}
void HttpCon::outPut()
{
/* QUrl *url=new QUrl("http://oa.ew.com.cn:7001/ewWeb/Main/index.action");
buffer = new QBuffer();
buffer->open(QIODevice::ReadWrite);
http->setHost(url->host(),url->port());
http->get(url->path());*/
/*qDebug()<<url->path();
QHttpRequestHeader header("POST", url->path());
header.setValue("Host", url->host());
header.setValue ("content-type", "application/x-www-form-urlencoded");
header.setValue ("accept-language", "zh-cn");
header.setValue( "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)");
http->setHost(url->host());
// http->post(url->path(),date->toUtf8());
qDebug()<<http->request(header) ;
http->post(url->path(),date->toUtf8());*/
QUrl *url=new QUrl("https://www.google.com/accounts/ClientLogin");
QString* date = new QString("bianhao=010202004090100011");
qDebug()<<url->host();
QByteArray postData = "\nservice=reader&";
postData += "Email=";
postData += QUrl::toPercentEncoding("
asdfasdf@gmail.com" );
postData += "&Passwd=";
postData += QUrl::toPercentEncoding("asdfasdfsadf" );
QHttpRequestHeader header("POST", url->path());
// header.setValue("Host", url->host());
header.setContentType("application/x-www-form-urlencoded");
// header.setValue ("Set-Cookie", "customer=huangxp; path=/beian; domain=.hd315.gov.cn");
QByteArray searchString("complete=1&hl=zh-CN&lr=lang_zh-CN%7Clang_zh-TW&newwindow=1&q=qhttp&start=10&sa=N");
// header.setValue("User-Agent","Mozilla/5.0 (PC; U; Intel; Linux; en) AppleWebKit/420+ (KHTML, like Gecko)");
header.setContentLength(searchString.length());
http->setHost(url->host(),QHttp::ConnectionModeHttps);
f.setFileName("results.html");
f.open(QIODevice::WriteOnly | QIODevice::Truncate);
mHttpReqId= http->request(header,postData,&f);
delete url;
}
void HttpCon::done(bool er )
{
qDebug() << "UPnP::Service: Got HTTP response."<<http->errorString();
if(er)
{
qDebug()<<"显示出错!";
http->close();
}else
{
qDebug()<<"http->bytesAvailable()"<<http->bytesAvailable();
/* QTextCodec::setCodecForCStrings(QTextCodec::codecForName("gbk"));
qDebug()<<"state"<<http->state();
QDateTime* date = new QDateTime();
QString str(date->currentDateTime().toString("yyyyMMddhhmmss"));
qDebug()<<str;
QFile *file = new QFile(str.toLower()+".txt");
file->open(QIODevice::ReadWrite);
//QByteArray httpStr = http->readAll();
QString result(http->readAll());
QTextStream outIn(file);
QRegExp* reg = new QRegExp("<[^>]*>");
QString s = result;
qDebug()<<s.replace(*reg, "");
outIn<<s.replace(*reg, "");
outIn.flush();
file->close();
qDebug()<<result;
//http->close();
file->close();
delete file;
file = 0;*/
}
//buffer->close();
//delete buffer;
//delete http;
}
void HttpCon::soltrequestFinished(int i, bool ff)
{
if (i != mHttpReqId) return;
if(ff)
{
f.close();
delete &f;
}
qDebug()<<"soltrequestFinished"<<i<<ff<<arr;
qDebug()<<"http->bytesAvailable()"<<http->bytesAvailable();
if(http->bytesAvailable()>0)
{
qDebug()<<http->readAll();
}
f.close();
}
void HttpCon:: mainresponseHeaderReceived(const QHttpResponseHeader & resp)
{
qDebug()<<"mainresponseHeaderReceived";
qDebug()<<"http->bytesAvailable()"<<http->bytesAvailable();
if(http->bytesAvailable()>0)
{
qDebug()<<http->readAll();
}
f.close();
}