IntroductionEver needed a quick way to get around to post text to a web server and get a response accordingly ? Look no further. In this article i give the reader a class with 2 methods for testing the availability of the connection and for posting information. Background I used this code to post major text files, line by line, of summaries on PocketPC (Windows Mobile) to a statistical web site via an ASP file. Using the code While under Windows, you can send information on an open socket and read information from the same spot. This is nothing new, but using a socket to post to an internet server can be tricky, especially getting the 'post' syntax absolutly flauless. As you can see on the header and body text below this paragraph, there is a macro SEND_RQ just for passing clear text values, so not to get misslead by any strange char in C/C++ strings. #define _DEBUG_PRINT(X) /* X */
#include <iostream>
#include <string>
#include <stdlib.h>
#include <assert.h>
#include <Winsock2.h>
#define VERSION_MAJOR 1;
#define VERSION_MINOR 1;
#define CRLF "\r\n" // carriage-return/line feed pair
#define SEND_RQ(MSG) send(sock,MSG, (int) strlen(MSG),0);using namespace std;class CHttp
{
public:
WSADATA WsaData;
<span class="Apple-tab-span" style="white-space: pre; "> </span>sockaddr_in sin;
<span class="Apple-tab-span" style="white-space: pre; "> </span>size_t sock ;
CHttp(void);
~CHttp(void);
int TestConnectionToHostName (char* hostname);
int Post (char* hostname, char* api, char* parameters, string& message);
}; #include "StdAfx.h"
#include "CHttp.h"CHttp::CHttp(void)
{
}CHttp::~CHttp(void)
{
}int CHttp::TestConnectionToHostName (char* hostname)
{
WSAStartup (0x0101, &WsaData);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
return -100;
}
sin.sin_family = AF_INET;
sin.sin_port = htons( (unsigned short)80);
struct hostent * host_addr = gethostbyname(hostname);
if(host_addr==NULL)
{
return -101;
}
sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list) ;
if( connect (sock,(const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1 )
{
return -102;
}
WSACleanup( );
return 0;
}int CHttp::Post (char* hostname, char* api, char* parameters, string& message)
{
WSAStartup (0x0101, &WsaData);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
return -100;
}
sin.sin_family = AF_INET;
sin.sin_port = htons( (unsigned short)80);
Read more: Codeproject
#include <iostream>
#include <string>
#include <stdlib.h>
#include <assert.h>
#include <Winsock2.h>
#define VERSION_MAJOR 1;
#define VERSION_MINOR 1;
#define CRLF "\r\n" // carriage-return/line feed pair
#define SEND_RQ(MSG) send(sock,MSG, (int) strlen(MSG),0);using namespace std;class CHttp
{
public:
WSADATA WsaData;
<span class="Apple-tab-span" style="white-space: pre; "> </span>sockaddr_in sin;
<span class="Apple-tab-span" style="white-space: pre; "> </span>size_t sock ;
CHttp(void);
~CHttp(void);
int TestConnectionToHostName (char* hostname);
int Post (char* hostname, char* api, char* parameters, string& message);
}; #include "StdAfx.h"
#include "CHttp.h"CHttp::CHttp(void)
{
}CHttp::~CHttp(void)
{
}int CHttp::TestConnectionToHostName (char* hostname)
{
WSAStartup (0x0101, &WsaData);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
return -100;
}
sin.sin_family = AF_INET;
sin.sin_port = htons( (unsigned short)80);
struct hostent * host_addr = gethostbyname(hostname);
if(host_addr==NULL)
{
return -101;
}
sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list) ;
if( connect (sock,(const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1 )
{
return -102;
}
WSACleanup( );
return 0;
}int CHttp::Post (char* hostname, char* api, char* parameters, string& message)
{
WSAStartup (0x0101, &WsaData);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
return -100;
}
sin.sin_family = AF_INET;
sin.sin_port = htons( (unsigned short)80);
Read more: Codeproject
0 comments:
Post a Comment