Qt|Http笔记-两种方式发送http协议数据,获取服务器数据(GET方法)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq78442761/article/details/87867991

目录

 

背景

演示

搭建Java Web环境

QTcpSocket获取服务器数据

QNetworkAccessManager获取服务器数据


 

背景

Qt中有2个方式可以实现HTTP协议的发送,一个是使用QNetworkAccessManager,这个是非常常见的,另外是使用QTcpSocket自己构造HTTP格式,也是可以的。

演示

web程序运行如下:

输入10086后:

使用telnet获取web网站数据:

发送命令以及接收如下:

使用QNetworkAccessManager获取服务器数据:

使用QTcpSocket获取数据:

搭建Java Web环境

java源码结构如下:

源码如下:

DataSource.java

package my;
 
import java.util.ArrayList;
 
public class DataSource {
 
	
	ArrayList<Record> m_records = new ArrayList<Record>();
	
	public DataSource(){
		
		m_records.add(new Record("XiaoMing", "10086"));
		m_records.add(new Record("XiaoHong", "10087"));
		m_records.add(new Record("XiaoGang", "10088"));
	}
	
	public Record query(String qq){
		
		for(Record e : m_records){
			
			if(e.qq.equals(qq)){
				
				return e;
			}
		}
		
		return null;
	}
 
	public class Record {
		
		public String name = "";
		public String qq = "0000";
		
		public Record(String name, String qq){
			
			this.name = name;
			this.qq = qq;
		}
	}
}

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  
    <form method="get" action="query.jsp" >
    	
    	输入QQ号
    	 
    	<input type="text"  name="qq" /> 
    	
    	<input type="submit" value="提交" />
    
    </form>
    
    
  </body>
</html>

query.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 
 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'query.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
 
  </head>
  
  <body>
 
    
    <%@ page import="my.*" %>
    
    <%
 
    	String qq = request.getParameter("qq");
    	
 
    	DataSource db = new DataSource();
    	DataSource.Record result = db.query(qq);
    	
    	
    	if(result == null)
    	{
    		out.print("<p> 不存在此记录 </p> ");
    	}    	
    	else
    	{
    		out.print("<p> 姓名: " + result.name + ", QQ: " + result.qq + "</p>");
    	}
     %>
     
  </body>
</html>

QTcpSocket获取服务器数据

程序结构如下:

widget.ui如下:

源码:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QTcpSocket;
QT_END_NAMESPACE

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

protected slots:
    void btnClicked();
    void onConnect();
    void onReadyRead();

private:
    Ui::Widget *ui;
    QTcpSocket *m_socket;
};

#endif // WIDGET_H

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QTcpSocket>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("QTcpSocket");
    m_socket = new QTcpSocket(this);
    connect(m_socket, SIGNAL(connected()), this, SLOT(onConnect()));
    connect(m_socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(btnClicked()));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::btnClicked()
{
    m_socket->connectToHost("47.101.40.99", 8080);
}

void Widget::onConnect()
{
    m_socket->write("GET /GetPostDemo/query.jsp?qq=10086\r\n"
                    "Host: 47.101.40.99\r\n\r\n");
}

void Widget::onReadyRead()
{
    qDebug() << QString::fromUtf8(m_socket->readAll());
}

QNetworkAccessManager获取服务器数据

程序结构如下:

widget.ui是这样的:

源码如下:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QNetworkAccessManager;
class QNetworkReply;
QT_END_NAMESPACE

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

protected slots:
    void btnClicked();
    void replyFinished(QNetworkReply *reply);

private:
    Ui::Widget *ui;
    QNetworkAccessManager *m_manager;
};

#endif // WIDGET_H

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QUrl>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("QNetworkAccessManager");
    m_manager = new QNetworkAccessManager(this);
    connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(btnClicked()));
    connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::btnClicked()
{
    m_manager->get(QNetworkRequest(QUrl("http://47.101.40.99:8080/GetPostDemo/query.jsp?qq=10086")));
}

void Widget::replyFinished(QNetworkReply *reply)
{
    qDebug() << QString::fromUtf8(reply->readAll());
}

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/87867991