Hi,
I need to send a flat file to http server. The file that needs to be sent is an xml file which is created by another routine. FTP is not an option, I have to send it to an http server via a multi-part form.
so far I have this code
from the server I get the following response however the file is not uploaded. \
Do I need to have some server side script too to grab all the data sent and write it to a file?
Really appreciate any help.
Thanks
Appreciate any help in this regards.
I need to send a flat file to http server. The file that needs to be sent is an xml file which is created by another routine. FTP is not an option, I have to send it to an http server via a multi-part form.
so far I have this code
Code:
int Send_to_DB_via_HTTP(string filename)
{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* server address */
unsigned short echoServPort; /* server port */
char *servIP; /* Server IP address (dotted quad) */
char *url;
char *echoString; /* String to send to echo server */
char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
unsigned int echoStringLen; /* Length of string to echo */
int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv()
and total bytes read */
int first_form_len=0;
int second_form_len=0;
unsigned count = 0;
char* Traveller_output_buffer;
FILE * pFile;
long lSize;
size_t result;
char* buf;
pFile = fopen ( filename.c_str(), "rb" );
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
Traveller_output_buffer = (char*) malloc (sizeof(char)*lSize);
if (Traveller_output_buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
// copy the file into the buffer:
result = fread (Traveller_output_buffer,1,lSize,pFile);
//ifstream file_to_send;
unsigned data_len;
servIP = "192.168.2.20";
echoServPort = 80;
url = "[URL unfurl="true"]http://something.dyndns.org";[/URL]
data_len = sizeof(char)*lSize;
/* Create a reliable, stream socket using TCP */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
cout << " socket () failed" << endl;
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(servlP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
cout<< "Error Connecting to " << servIP << endl;
else
cout << "Connection Successfull to " << servIP << endl;
first_form_len += snprintf(
&buffer1[first_form_len],
sizeof(buffer1) - first_form_len,
"Content-Disposition: form-data; name=\"flat_file\"; filename=\"new.dat\"\r\n" );
first_form_len += snprintf(
&buffer1[first_form_len],
sizeof(buffer1) - first_form_len,
"Content-Type: text/plain\r\n\r\n" );
cout << first_form_len << endl;
//Start the form off with the boundary string
second_form_len += snprintf(
&buffer1[first_form_len+second_form_len],
sizeof(buffer1) - (first_form_len+second_form_len),
"%s%s\r\n",
"--",BOUNDARY_STRING );
second_form_len += snprintf(
&buffer1[(first_form_len+second_form_len)],
sizeof(buffer1) - (first_form_len+second_form_len),
"Content-Disposition: form-data; name=\"filename\"\r\n\r\n%s_auto_end\r\n",
syssn_no );
//End of multi-part form
second_form_len += snprintf(
&buffer1[(first_form_len+second_form_len)],
sizeof(buffer1) - (first_form_len+second_form_len),
"%s%s%s\r\n",
"--",BOUNDARY_STRING,"--" );
// Put together the headers for HTTP POST
count = snprintf(
&buffer[0],
sizeof(buffer),
"POST %s HTTP/1.0\r\n",
url );
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"Accept-Language: en-us\r\n" );
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"Content-Type: multipart/form-data; boundary=%s\r\n",
BOUNDARY_STRING);
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"User-Agent: Mozilla/3.01 (compatible; %s-%s/%d.%d.%02d r%d)\r\n");
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"Host: %s\r\n",
servIP);
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"Pragma: no-cache\r\n" );
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"Content-Length: %d\r\n",
data_len+first_form_len+second_form_len );
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"\r\n" );
cout << buffer1 << endl;
string status;
//Send the "POST" header to the HTTP server.
if (send(sock, buffer, count, 0) == -1) {
//status = perror("send");
cout << "Stats Worker: received socket error %d on header send\n" << endl;
return -1;
}
else
{
cout << "data Sent" << endl;
}
//Send the first part of the form data to the HTTP server.
if (send(sock, buffer1, first_form_len, 0) == -1) {
//status = CK_Get_last_error();
cout << "Stats Worker: received socket error %d on header send\n" << endl;
return -1;
}
//Now send the flat file
if (send(sock, Traveller_output_buffer, data_len, 0) == -1) {
//status = CK_Get_last_error();
cout << "Stats Worker: received socket error %d on message send\n" << endl;
return -1;
}
//Send the last part of the form data to the HTTP server.
if (send(sock, buffer1+first_form_len, second_form_len, 0) == -1) {
//status = CK_Get_last_error();
cout << "Stats Worker: received socket error %d on header send\n" << endl;
return -1;
}
else
{
cout << "Data has been Sent Successfully" << endl;
}
int n = recv(sock, buf, sizeof(buf), 0);
while (n > 0) {
printf(buf);
n = recv(sock, buf, sizeof(buf), 0);
}
close(sock);
}
Code:
HTTP/1.1 200 OK
Date: Sat, 06 Sep 2008 17:34:48 GMT
Server: Apache/2.2.3 (Debian) PHP/5.2.0-8+etch10
X-Powered-By: PHP/5.2.0-8+etch10
Content-Length: 28
Connection: close
Content-Type: text/html; charset=UTF-8
Really appreciate any help.
Thanks
Appreciate any help in this regards.