Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
 #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();

}
Posted
Updated 19-Apr-16 13:10pm
v2
Comments
CPallini 18-Apr-16 6:45am    
Low level code might be inelegant, however it usually is more efficient than higher level one. Why do you think your code is inefficient?
Member 12383893 19-Apr-16 9:33am    
When it connects to the server it does not do the request i want it to do nor do i get the same response i get when i use curl or httpbin website to send the request. When ever i run the code it says bad request
leon de boer 19-Apr-16 15:15pm    
Sorry that it not the codes fault it is yours .. YOU ARE SENDING AN INVALID REQUEST.

The reason it is invalid I can guess at, because you are talking to port 443 and this site "https://mapi.alipay.com", so I am pretty sure it is a HTTPS server not a HTTP server.

To connect to a HTTPS server you must create a public key certificate from a certificate provider and that certificate must trusted by both server and client. If it is a HTTPS server it's never going to accept a single command without the security connection. So no your code from HttpBin will not work until you fix the security connection up.

So I suspect the problem is nothing to do with low level code but more your lack of understanding.

Your next step is to probably talk to alipay.com and find out what certificates they will accept.
Member 12383893 20-Apr-16 4:39am    
thank you very much i will get to that as soon as i can.

1 solution

Since curl works for you, use the same library as curl - libcurl.

LibCurl - a c programming library for client/server communications[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900