Send GET and POST requests in C++
In this tutorial, we will see how to send GET and POST requests in C++.
The usage of these requests majorly in the field of www and it uses various protocols.
GET method in C++
HTTP get request is used to get data from the web-server. It has no side effects and it is not supposed to change anything on the server.
It can be executed any number of times without side effects.
If you click a hyperlink or you type an URL in an address bar and hit enter it will issue a gate method.
Send GET request in
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
char p_request[100]={0};
char p_resourcepath[]="2019/07/creating-xml-request-in-cpp-for-server.html";
char p_hostaddress[]="www.cplusplus.com";
sprintf(p_request, "GET /%s HTTP/1.1\r\nHost: %s\r\nContent-Type: text/plain\r\n\r\n", p_resourcepath, p_hostaddress);
cout<<"Created Get Request is as follows:"<<"\n";
cout<< p_request;
return 0;
}
POST method in C++
POST method is used for submitting data to the server. This method is used where we want to post a message.
the POST method is more secure as compared to GET method.
Send POST request in C++
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
char a_XmlData[]="<body><name>amss</name><age>19</age></body>";
char a_XmlRequest[251]={0};
char a_ServiceMethod[]="applicationformat.svc/getdetail";
char a_Request[151]={0};
char a_HostIp[31]="76.127.24.111";
char a_Port[]="81";
sprintf(a_Request,"http://%s:%s/%s/%s HTTP/1.1",a_HostIp,a_Port,a_ServiceMethod);
cout<<"Method and Resource path is below:";
cout<<a_Request;
strcat(a_HostIp,":");
strcat(a_HostIp,a_Port);
cout<<"HOST header is below:"<<endl;;
cout<<a_HostIp;
sprintf(a_XmlRequest,"POST %s\r\nHost: %s\r\nContent-Type: application/xml\r\nContent-Length: %d\r\n\r\n%s\r\n",aszRequest,aszHostIp,strlen(aszXmlData),aszXmlData);
cout<<"POST Request which send to the server:"<<endl;
cout<<a_XmlRequest;
return 0;
}Also read:
Leave a Reply