-1

我正在开发一个简单的应用程序,它将图像上传到 yfrog.com。(这些图像将反映在 twitter 帐户中)。这是我的代码。但它不工作。我没有得到服务器的响应。

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest request(QUrl("http://yfrog.com/api/uploadAndPost"));

QByteArray data;
QUrl params,params1;
QFile file("some image path");
QString boundary("-----abcde12345");
QString body = "\r\n--" + boundary + "\r\n";

params.addQueryItem("username",twitterusername);
params.addQueryItem("password",twitterpassword);
params.addQueryItem("message",some message...);
params.addQueryItem("key",mydeveloperkey);


data.append(body);
data.append(params.toString());
QByteArray ba;
ba=file.readAll();
QString body1(ba);
params1.addQueryItem("media",body1);
data.append(params1.toString());
data.append(body);


request.setRawHeader("Content-Type","multipart/form-data; boundary=-----abcde12345");
request.setHeader(QNetworkRequest::ContentLengthHe ader,data.size());

QNetworkReply *reply = manager->post(request,data);

reply->waitForReadyRead(-1);
qDebug() << "replay :"<<reply->readAll();

如果我检查了来自 Wireshark 的请求 TCP 数据包,它会给出一条错误消息,例如“格式错误的数据包”。

供参考:http ://code.google.com/p/imageshacka...GuploadAndPost

请任何机构对此提供帮助。我在哪里做错了?

4

1 回答 1

0

QNetworkReply::waitForReadyRead does not have an implementation so it always refers the base class waitForReadyRead() (in this case QIODevice). In the base class, you will see that waitForReadyRead always returns FALSE.

From the docs, you would have to instead use the readRead() signal in QNetworkReply and read the data when the slot is called.

 QNetworkReply *reply = manager->get(request);
 connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
于 2010-11-12T22:52:05.037 回答