如何使用C ++向服务器发送http请求 [英] How do I send a http request to a server using C++
本文介绍了如何使用C ++向服务器发送http请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在命令行上使用curl完成了这个,但现在尝试在代码中实现它:
我正在尝试连接到支付宝并将其直接发送到服务器
Iv done this using curl on the command line but now trying to implement it in code:
I am trying to connect to alipay and send this directly to the server
https://mapi.alipay.com/gateway.do?/_input_charset=UTF-8¤cy=USD¬ify_url=10.237.221.84:80&out_trade_no=123456789&partner=2088101122136241&sign=760bdzec6y9goq7ctyx96ezkz78287de&subject=Coke&sign_type=MD5&service=create_forex_trade&total_fee=0.01
但我的代码是低级别的方式有效地做到这一点有人可以帮助我吗?
我有什么尝试过:
but the code i have is way to low level to do this efficiently could someone help me??
What I have tried:
#include <qcoreapplication>
#include "sockettest.h"
#include <iostream>
#include <string>
#include <qcoreapplication>
#include <qcryptographichash>
#include <qdebug>
#include <qurl>
#include <qnetworkrequest.h>
#include <qnetworkaccessmanager.h>
#include <qnetworkreply.h>
SocketTest::SocketTest(QObject *parent) : QObject(parent)
{
}
void SocketTest::test()
{
socket = new QTcpSocket(this);
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
qDebug() << "Connecting...";
socket->connectToHost("mapi.alipay.com",443);
if (!socket->waitForConnected(1000))
{
qDebug() << "Error: \n\n" << socket->errorString();
}
}
void SocketTest::connected()
{
qDebug() << "Connected!";
QByteArray output;
QByteArray alipay_tran_req;
QByteArray alipay_tran_req2;
alipay_tran_req = "https://mapi.alipay.com/gateway.do?/_input_charset=UTF-8¤cy=USD¬ify_url=10.237.221.84:80&out_trade_no=123456789&partner=2088101122136241&";
alipay_tran_req2 ="&subject=Coke&sign_type=MD5&service=create_forex_trade&total_fee=0.01";
QCryptographicHash CalculateMd5(QCryptographicHash::Md5);
CalculateMd5.addData(output);
output = alipay_tran_req + "sign=" + (CalculateMd5.result().toHex())+ alipay_tran_req2;
qDebug() << "Input String" << output;
qDebug() << socket->write(alipay_tran_req) << socket->write(output);
socket->write(output);
}
void SocketTest::disconnected()
{
qDebug() << "Disconnected!\n";
}
void SocketTest::bytesWritten(qint64 bytes)
{
qDebug() << "\nWe Wrote: \n" << bytes;
}
void SocketTest::readyRead()
{
qDebug() << "\nReading!\n";
qDebug() << socket->readAll();
}
推荐答案
因为curl适合你,所以使用与curl相同的库 - libcurl。
LibCurl - 用于客户端/服务器通信的交互式编程库 [ ^ ]
Since curl works for you, use the same library as curl - libcurl.
LibCurl - a c programming library for client/server communications[^]
这篇关于如何使用C ++向服务器发送http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文