Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How can I get things from website
-
@victor-wang said in How can I get things from website:
"connect_url" will print the number 200 out to make sure the Url is connecting successfully.
Yes, 200 means "HTTP OK": http://www.restapitutorial.com/httpstatuscodes.html
Your code is connecting to the server OK.
void MainWindow::replyFinished(QNetworkReply *reply) { qDebug("connect content"); QTextCodec *codec=QTextCodec::codecForLocale(); int connect_url=reply -> attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug()<<connect_url; qDebug("here"); }
You need to read the data from the QNetworkReply. Call
reply->readAll()
to get the data.Also, remember to delete your QNetworkReply after you have finished, or else you will have a memory leak. See the QNetworkReply documentation: http://doc.qt.io/qt-5/qnetworkreply.html
@victor-wang said in [How can I get things from website](/topic/77075/how-can-i-get-things-from-
I should get a file
As I said above, you must call
reply->readAll()
to get the data. Then, write the data into a file using a QFile.
-
@JKSH
you mean i have to add this after the connect function?qDebug()<<reply->readAll(); reply->deleteLater();
Is that readAll will print the information out?
Or i have to using qDebug to print it out?
-
@victor-wang said in How can I get things from website:
@JKSH
you mean i have to add this after the connect function?qDebug()<<reply->readAll(); reply->deleteLater();
I mean you have to add this inside your
replyFinished()
slot.Are you familiar with asynchronous programming? Networking is asynchronous. When you call
QNetworkAccessManager::get()
, it starts downloading. However, it doesn't wait for the download to finish. Instead, QNetworkAccessManager and QNetworkReply WILL emit thefinished()
signal when the download finishes. So, you can read the downloaded data from inside the connected slot.Is that readAll will print the information out?
Or i have to using qDebug to print it out?readAll() gives you the data in a QByteArray. It doesn't print anything. Do you know how to use QByteArray?
qDebug prints to the console.
-
I am not quite sure that i'm using the right way.
I write this code into replyFinished();QByteArray output; output = reply -> readAll(); qDebug()<<output; reply->deleteLater();
Am i right?
Sorry i'm new in Qt please be patient to me:(
-
@victor-wang said in How can I get things from website:
I write this code into replyFinished();
QByteArray output; output = reply -> readAll(); qDebug()<<output; reply->deleteLater();
Am i right?
Yes, that is right :)
Now, you can save
output
into a file, using QFile.Sorry i'm new in Qt please be patient to me:(
No problem! We are happy to help beginners. I think you are learning well.
-
@JKSH
I this function will show the content out?qDebug()<<output;
If it is why it showed NULL on the console. here
I thought it will print the Url information which i had just gave you "Updated ok!".
-
I add this after connect instead of replyFinished slot.
file.setFileName("/home/check.txt"); if(!file.exist)qDebug("not exist"); if(!file.open(QIODevice::WriteOny | QIODevice::Text))qDebug("not write success"); stream.setDevice(&file); stream<<out; file.close;
But when i went to cat the check.txt there is nothing in the file.
And i add this in my replyFinished slot.
out=reply->readAll(); qDebug()<<out; reply->deleteLater();
The "QByteArray out" i had declared it in the mainwidow.h for global type.
Did i need to put in the private place or the public place in the mainwindow.h?Why i still didn't see anything in my file?
-
@victor-wang said in How can I get things from website:
I this function will show the content out?
qDebug()<<output;
Yes.
The "QByteArray out" i had declared it in the mainwidow.h for global type.
Did i need to put in the private place or the public place in the mainwindow.h?No, no need.
@victor-wang said in How can I get things from website:
I add this after connect instead of replyFinished slot.
That definitely won't work, because you haven't received the data yet.
Remember, networking is asynchronous. You can only read the data after the
finished()
signal is emitted.If it is why it showed NULL on the console. here
I thought it will print the Url information which i had just gave you "Updated ok!".
...
Why i still didn't see anything in my file?This is where you need to start debugging and testing.
- What do you get when you type your URL into your browser?
- What happens when you change the URL to http://lists.qt-project.org/mailman/listinfo ?
- Is your QNetworkRequest constructed correctly?
- Is your device on the same subnet as the server?
-
@JKSH
Is that you mean i have to add this QFile function in the replyFinisheed() slot?
Or i have to do it when the finished signal is up?
If it is how can i judge the signal and where do i have to add the function in?For the first of debugging and testing.
I will get this on my browser. hereFor the second one, i'm not quite sure what does that mean.
For third one, what does"QNetworkRequest constructly correctly"mean?
I thought i am using the library that Qt have already constructed.For forth one, I'm sure i am on the same subnet because i can send this commend to my console
wget http://192.168.100.59/PLMS/TestCheck.asp?LSN=201503003009&EmpId=we9999&CPID=OS&CPKey=95Z0000000AF
And i will get the file and the content will show "updated ok".
-
@JKSH
I had add qDebug() function in replyFinished() slot.
Here is my code.According to what you said it will print something out if i am in the same subnet.
And i'm sure that i am in the same subnet but why it still print NULL out like this?According to the picture i gave you, i assume that it will return "Updated ok!" back to me.
-
@victor-wang said in How can I get things from website:
Or i have to do it when the finished signal is up?
@JKSH already said several times that you have to do it in the slot connected to the finished() signal. So, yes do it in when you get finished() signal.
To your code: you call readAll twice - that is probably the reason why qDebug() << out; prints nothing. You should call readAll only once and then print the data.
-
@jsulm
That mean i have to add QFile function into replyFinished() SLOT right?ReadAll function actually it call twice by itself and i don't know why it will happened.
I'm sure that i just call one time for the ReadAll function.
This is my code here.But when I add it into replyFinished() slot, it cannot connect to server.
The connect_url will return 0 for the second time to me like this.
What can i do?
-
@victor-wang YES
"ReadAll function actually it call twice by itself" - how can a function call itself?
You do it twice, please check your own code again. First time: line 1999, second time line 2013.
-
@jsulm
Thank you for telling me!
But now i still get nothing.
I had check several time that i can wget the Url on the console.
And i'm sure it is the right subnet and Url.
And i checked the check.txt file can be write nor read.
Why it still print NULL out?
-
@victor-wang show your code
-
@jsulm
Here is the code for replyFinished() slot.This is where i called the Finished() signal and replyFinished() slot via connect
-
@victor-wang You cannot write in /home! (line 2019)
It should be /home/YOUR_USER_NAME/check.txt
-
@victor-wang What do you get if you use wget with that URL? Any data?
-
@jsulm
I will get a file like this.i will get the file called "TestCheck.asp?LSN=201503003009&EmpId=we9999&CPID=OS&CPKey=95Z0000000AF"
And i can use vi to see the content inside like this.
I changed the path to /home/root/check.txt still got nothing in it.
-
@victor-wang You should print out reply->error()
If everything was fine then it should print out 0 (QNetworkReply::NoError)
-
@jsulm
It shows no error.
That means everything was fine.
But why still have nothing?
Is there any possible because i receive a file?
-
@victor-wang Is that out variable a QByteArray?
Also, what exactly does qDebug()<<out print out?
-
-
@victor-wang can you do qDebug()<<out.size();?
-
Yes, I can do that.
this is my code hereAnd it shows 0.
Is that mean there is nothing in it?
-
@victor-wang said in How can I get things from website:
If it is how can i judge the signal and where do i have to add the function in?
You make a signal-slot connection, using
QObject::connect()
. When the signal is emitted, the connected slot will run automatically.This is a core feature of Qt. I recommend you spend some time to do tutorials to understand how to use signals and slots.
For the first of debugging and testing.
I will get this on my browser. hereOK, good. That means the server will give you text data.
For the second one, i'm not quite sure what does that mean.
Your code has
QUrl("http://192.168.100/59/...")
. Change this toQUrl("http://lists.qt-project.org/mailman/listinfo")
for testing.For third one, what does"QNetworkRequest constructly correctly"mean?
I thought i am using the library that Qt have already constructed.When you call
QNetworkRequest(QUrl(...));
you are constructing a QNetworkRequest.Let's check that your QNetworkReply is what you expect. Put this in your
replyFinished()
slot:qDebug() << reply->isFinished(); qDebug() << reply->error(); qDebug() << reply->request(); qDebug() << reply->url(); qDebug() << reply->rawHeaderPair();
For forth one, I'm sure i am on the same subnet because i can send this commend to my console
wget http://192.168.100.59/PLMS/TestCheck.asp?LSN=201503003009&EmpId=we9999&CPID=OS&CPKey=95Z0000000AF
And i will get the file and the content will show "updated ok".
OK, good.
-
for your Xming copy/paste problem to windows, take a look to this topic:
https://superuser.com/questions/440128/xming-clipboard-only-works-one-way
That should enable you to poste your code as text for us and not as pictures.
-
@JKSH
I add the qDebug in the replyFiniched() slot but i got some errors.
HereWhy is this happened?
-
@JKSH
I have try to get from the Url which you gave me for test.
And i will get this information here.So is that mean the code is not wrong?
-
@victor-wang said in How can I get things from website:
@JKSH
I have try to get from the Url which you gave me for test.
And i will get this information here.So is that mean the code is not wrong?
Yes, your code is working correctly. Go to http://lists.qt-project.org/mailman/listinfo using your browser and view that web page's source (Ctrl+U in Google Chrome) -- it's the same as your last screenshot.
Your code is not wrong. Your device (192.168.100.59) probably returns data in a different way. Fix the errors below to investigate.
@victor-wang said in How can I get things from website:
@JKSH
I add the qDebug in the replyFiniched() slot but i got some errors.
HereOops, sorry... QNetworkRequest can't be printed through qDebug(). Just delete this line:
qDebug() << reply->request();
The other one should be
rawHeaderPairs()
, not rawHeaderPair()
-
I had change the Url back to the original one.
And i get thisIs that mean i had receive something?
If it is why i didn't see any file that should get from the Url?
-
@victor-wang said in How can I get things from website:
I had change the Url back to the original one.
And i get thisIs that mean i had receive something?
Yes.
If it is why i didn't see any file that should get from the Url?
Because your code contains the wrong URL. You want LSN=201503003009, right?
-
@JKSH
Actually, if i connect success the URl i set will have the record.
But i just check it, there is nothing on it.
I don't know what to do now.
-
@JKSH
I solved the problem!
I 'm too stupid that i type the wrong vocabulary.
It's Empid not Empld in the Url.
Thank you so much for all your help!
-
@JKSH
There is another questions.manager->get(QNetworkRequest(QUrl(http://www1.winmate/PLMS/TestCheck.asp?LSN=201503003009&Empid=we9999&CPID=OS&CPKey=95Z0000000AF)));
what if the Empid is not the static one.
It will let the guest type there own id.
If i want to doing that how can i do it?Can i type like this?
manager->get(QNetworkRequest(QUrl("http://www1.winmate/PLMS/TestCheck.asp?LSN=201503003009&"+"Empid="+ID"&CPID=OS&CPKey=95Z0000000AF")));
-
@victor-wang You can do it like this (Empid= can be just part of the first string - no need for +"Empid=").
Take a look at http://doc.qt.io/qt-5/qstring.html#arg
-
@jsulm @JKSH
I've got another question!
I have 10 different Url wanna connect and get information down.
And i'm using for() to do it.Here is my code.But it will crash and will not connect to the server.
I assume that it because for() is doing too quick.
So i add this inside the casereply->isFinished();
But it will crash in the beginning.
What else method can i try or what to fix in my code?
Do i need to add the switch case into replyfinished() slot instead of connect?Please help!
-
@victor-wang The problem isn't that for loop is too fast (how could it be too fast?).
Starting with case one you have:reply->isFinished();
How do you initialize reply? Most probably it is not initialized and you're dereferencing dangling pointer.
Second question: why do you call isFinished() on reply? isFinished() is a getter method which you can use to check whether the reply is finished or not. But you do not use its return value, so the call is completely useless.
In general: if your app is crashing you should first debug it before asking in a forum.
So, please remove that useless reply->isFinished(); and debug your code to see where exactly it is crashing and why.
-
@victor-wang
To add to @jsulm :make a make a proper Network function something along the line of:
void getUrlData(QUrl url){ //Do Stuff to call data from one URL }
Place all your QUrls(10?) into a
QList<QUrl>
Now link the
IsFinished
Signal of your reply to Funktion to call the next URL request from your list.Dont forget to add a cancel condition, when you reach the end of your list.
Now, start the download chain by calling the first element of your list by hand/button clicked.
-
@J.Hilk
Is that mean that i have to connect IsFinished signal and getUrlData slot in the replyFinished() slot?
What is that mean put in the QList?
There is no IsFinished () signal for reply, just have finished signal.