Originally Posted by
QPlace
I have a web service in ASP.Net, getManagers, that accepts one parameter, UserName and returns a list of strings to a client.
I am trying to build the POST request from QT and I will appreciate any comments or suggestions on this subject
Here is what I do:
QHttpRequestHeader header("POST", "/StoreManager/getManagers.asmx");
header.setValue("Host", "localhost:1048");
header.setValue("charset", "utf-8");
header.setContentType("application/soap+xml");
m_http.setHost("http://localhost:1048");
m_http.request(header, "UserName=QPlace");
connect((QObject*)(&m_http), SIGNAL(requestFinished ( int id, bool error )), this, SLOT(ThisReqestIsCompleted ( int id, bool error )));
First problem is that ThisReqestIsCompleted is never called. But I am unsure if I am doing the call to the service correctly. All comments and suggestions are greatly appreciated
Query is incorrect in terms of HTTP proto. let's look at peace of code that sending file to server:
QByteArray begin_data
="--"+boundary
+"\r\nContent-Disposition: form-data; name=\"";
begin_data+=conf->get_config_for("FILE_INPUT_NAME");
begin_data+="\"; filename=\"test.txt\"\r\n\r\n";
content_data=read->readAll();
content_data+="\r\n";
QByteArray passwd_data
= "--"+boundary
+"\r\nContent-Disposition: form-data; name=\"code\"\r\n\r\n";
passwd_data+=conf->get_config_for("ACCESS_CODE");
QByteArray data
= begin_data
+content_data
+passwd_data
+end_data;
header.setValue("Host",conf->get_config_for("HOST_NAME"));
header.setValue("Content-type","multipart/form-data; boundary="+boundary);
header.setValue("Content-Transfer-Encoding","binary");
if(conf->get_config_for("PROXY_HOST").size()!=0) {
http->setProxy(conf->get_config_for("PROXY_HOST"),
conf->get_config_for("PROXY_PORT").toInt(),
conf->get_config_for("PROXY_AUTH_USERNAME"),
conf->get_config_for("PROXY_AUTH_PASSWORD"));
}
http->setHost(conf->get_config_for("HOST_NAME"));
current_request=http->request(header,data);
req_finish.lock();
QByteArray boundary = QByteArray::number(reinterpret_cast<long long>(this));
QByteArray begin_data="--"+boundary+"\r\nContent-Disposition: form-data; name=\"";
begin_data+=conf->get_config_for("FILE_INPUT_NAME");
begin_data+="\"; filename=\"test.txt\"\r\n\r\n";
QByteArray content_data;
content_data=read->readAll();
content_data+="\r\n";
QByteArray passwd_data = "--"+boundary+"\r\nContent-Disposition: form-data; name=\"code\"\r\n\r\n";
passwd_data+=conf->get_config_for("ACCESS_CODE");
QByteArray end_data="\r\n--"+boundary+"--\r\n";
QByteArray data = begin_data+content_data+passwd_data+end_data;
QHttpRequestHeader header("POST",conf->get_config_for("HOST_UPLOAD_PATH"));
header.setValue("Host",conf->get_config_for("HOST_NAME"));
header.setValue("Content-type","multipart/form-data; boundary="+boundary);
header.setValue("Content-Transfer-Encoding","binary");
if(conf->get_config_for("PROXY_HOST").size()!=0) {
http->setProxy(conf->get_config_for("PROXY_HOST"),
conf->get_config_for("PROXY_PORT").toInt(),
conf->get_config_for("PROXY_AUTH_USERNAME"),
conf->get_config_for("PROXY_AUTH_PASSWORD"));
}
http->setHost(conf->get_config_for("HOST_NAME"));
current_request=http->request(header,data);
req_finish.lock();
To copy to clipboard, switch view to plain text mode
and finish signal handler:
void http_daemon::request_signal(int r_numb,bool err) {
log<<"Req fihish recvd:"<<r_numb<<" with err state:"<<err<<"\n";
log<<QString(http->readAll())<<"\n";
if(current_request==r_numb) {
// This req is waiting for req. Unlock main send thread!
req_finish.unlock();
}
}
void http_daemon::request_signal(int r_numb,bool err) {
log<<"Req fihish recvd:"<<r_numb<<" with err state:"<<err<<"\n";
log<<QString(http->readAll())<<"\n";
if(current_request==r_numb) {
// This req is waiting for req. Unlock main send thread!
req_finish.unlock();
}
}
To copy to clipboard, switch view to plain text mode
Mutex req_finish used to block send function(thread) (first pease of code) until req is finished. (or some data, in your case QHttp object, would been destroyed after function exit)
PS
From QHttp docs:
The class works asynchronously, so there are no blocking functions. If an operation cannot be executed immediately, the function will still return straight away and the operation will be scheduled for later execution. The results of scheduled operations are reported via signals. This approach depends on the event loop being in operation.
Bookmarks