Qt与Tomcat服务器通信实例 post方法提交
1. 利用Qt 網(wǎng)絡(luò)類:
?QNetworkAccessManager 、QNetworkReply、?QNetworkRequest
2. 要知道的知識(shí):
每一種語(yǔ)言在實(shí)現(xiàn)網(wǎng)絡(luò)接口的時(shí)候,都會(huì)有自己的實(shí)現(xiàn)類。而我們又知道網(wǎng)絡(luò)協(xié)議一般來(lái)說(shuō)分為7層,所有的應(yīng)用都是建立在此基礎(chǔ)之上,它們是標(biāo)準(zhǔn)協(xié)議,每一種語(yǔ)言都通用且必須嚴(yán)格遵守。舉例說(shuō)明,譬如socket工作在傳輸層,Http中的post/get工作在應(yīng)用層。所以我們可以得出這樣的結(jié)論:無(wú)論java、c、c++、Qt 或者其他語(yǔ)言,只要他們實(shí)現(xiàn)了socket、http,那么一定可以實(shí)現(xiàn)數(shù)據(jù)通信。本文章將實(shí)例講解 Qt的http類與Tomcat服務(wù)器中的java servelt類之間的數(shù)據(jù)通信。
提示:本實(shí)例僅為測(cè)試通信,界面一個(gè)按鈕,一個(gè)文本框(暫時(shí)無(wú)用,待完善)。Qt提交數(shù)據(jù),tomcat中java類接收輸出
運(yùn)行結(jié)果:(本實(shí)驗(yàn)用的交叉編譯器,linux下實(shí)驗(yàn)過(guò)成功運(yùn)行)
Qt界面
myeclipse輸出:
直接放代碼,注釋在代碼中:
本文的所有工程源碼在資源中,可以下載參考:http://download.csdn.net/detail/guomutian911/7115883
Qt代碼:
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> #include <QDebug> #include <QtGui> #include <QNetworkAccessManager> #include <QNetworkReply> #include <QNetworkRequest> #include <QtNetwork>namespace Ui {class MainWindow; }class MainWindow : public QMainWindow {Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();QString str;public:// QPushButton* pushButton;private:Ui::MainWindow *ui;QNetworkAccessManager *nam;QNetworkReply *reply;public slots:void push();};#endif // MAINWINDOW_Hmainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :nam(new QNetworkAccessManager(this)), //必須有,要不執(zhí)行一次出錯(cuò)QMainWindow(parent),ui(new Ui::MainWindow) {ui->setupUi(this);QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(push())); //連接信號(hào)、槽}MainWindow::~MainWindow() {delete ui; }void MainWindow::push() {qDebug() << "hello world";QUrl url("http://192.168.1.100:8080/myhttp/LoginAction"); //服務(wù)器端servelt接收地址QNetworkRequest req;req.setUrl(QUrl(url));req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded"); //此句交叉編譯時(shí)必須有,否則報(bào)錯(cuò)QByteArray append("username=admin&password=123456"); //定義字符串QNetworkReply *reply = nam->post(req, append); //以post方式發(fā)送數(shù)據(jù)}
服務(wù)器端myeclipse中代碼:(注釋內(nèi)容較多,此代碼在原來(lái)項(xiàng)目中修改,可以忽略)
package guo; /*服務(wù)器端 收發(fā)* request 收* response 發(fā)* */ import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //繼承HttpServlet,為了生成HttpServletRequest、HttpServletResponse對(duì)象 public class LoginAction extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubsuper.doGet(req, resp);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// TODO Auto-generated method stubresponse.setContentType("text/html;charset=utf-8");//設(shè)置應(yīng)答內(nèi)容,字體編碼request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");//PrintWriter 基于字符的輸出流,可以輸出字符或字符串整型等數(shù)據(jù)。輸出的目標(biāo)可以是磁盤文件、其他輸出流//從response獲得字符流 ,out由web容器創(chuàng)建PrintWriter out = response.getWriter();String username = request.getParameter("username");//獲取請(qǐng)求參數(shù)String password = request.getParameter("password");//判斷用戶名密碼是否正確/*if(username.equals("admin") && password.equals("123")) {out.print("Login succeeded!");//給輸出目標(biāo)輸出字符信息//out.print("用戶名"+username);}else {out.print("Login failed!");}*/out.print(username);out.print(password);System.out.println(username);System.out.println(password);out.flush();//清除字符流緩存out.close();//關(guān)閉字符流 }}作者參考博文:
1.http://blog.csdn.net/chenlong12580/article/details/7393563
2.http://blog.csdn.net/ultimater/article/details/8762237
總結(jié)
以上是生活随笔為你收集整理的Qt与Tomcat服务器通信实例 post方法提交的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java 中对象引用,以及对象赋值
- 下一篇: 单片机串行收发电路制作记录