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

目录

 

背景

演示

搭建Java Web环境

QTcpSocket获取服务器数据

QNetworkAccessManager获取服务器数据


 

背景

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

 

演示

web程序运行如下:

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

输入10086后:

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

 

使用telnet获取web网站数据:

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

发送命令以及接收如下:

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

 

使用QNetworkAccessManager获取服务器数据:

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

使用QTcpSocket获取数据:

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

 

搭建Java Web环境

java源码结构如下:

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

 

源码如下:

DataSource.java

package my;
 
import java.util.ArrayList;
 
public class DataSource {
 
	
	ArrayList m_records = new ArrayList();
	
	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+"/";
%>
 


  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
  
    
输入QQ号

query.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 
 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 


  
    
    
    My JSP 'query.jsp' starting page
    
	
	
	    
	
	
	
 
  
  
  
 
    
    <%@ page import="my.*" %>
    
    <%
 
    	String qq = request.getParameter("qq");
    	
 
    	DataSource db = new DataSource();
    	DataSource.Record result = db.query(qq);
    	
    	
    	if(result == null)
    	{
    		out.print("

不存在此记录

"); } else { out.print("

姓名: " + result.name + ", QQ: " + result.qq + "

"); } %>

 

QTcpSocket获取服务器数据

程序结构如下:

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

widget.ui如下:

源码:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 

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 

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 
#include 
#include 

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获取服务器数据

程序结构如下:

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

widget.ui是这样的:

源码如下:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 

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 

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 
#include 
#include 

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());
}

 

 

 

你可能感兴趣的:(C/C++,Java,C/C++&JAVA,http/https)