Hi,
I have a C++ routine(client-side) which uploads an xml file to a web server by making a socket connection and sending all the post request through that socket.
On the server side I have a cgi script which receives all the data and creates a file in the specified directory.
If I am uploading only the file all works well, however I want to send data of other fields too (field1, field2 ..etc), this fails the post request and even the file is not uploaded.
I think it has something to do with the Boundary Strings that need to be sent with each part of the form-data.
However I am unable to understand how to do that.
Greatly appreciate any help.
Here is my code snippet for preparing the post request for sending the form-data -
int Send_to_DB_via_HTTP(char* Traveller_output_buffer, unsigned data_len, string fileName, string servIP, string url)
-
{
-
-
int sock; /* Socket descriptor */
-
struct sockaddr_in echoServAddr; /* server address */
-
unsigned short echoServPort; /* server port */
-
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;
-
int hidden_data_len=0;
-
unsigned count = 0;
-
int count1 = 0;
-
int count2 = 0;
-
char buf[2000];
-
int n;
-
const char *m_servIP;
-
const char *m_url;
-
m_servIP = servIP.c_str();
-
m_url = url.c_str();
-
echoServPort = 80;
-
-
/*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 = PF_INET; /* Internet address family */
-
echoServAddr.sin_addr.s_addr = inet_addr(m_servIP); /* Server IP address */
-
echoServAddr.sin_port = htons(echoServPort); /* Server port */
-
-
/* Establish the connection to the echo server */
-
int k;
-
k = connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr));
-
-
if (k < 0)
-
{
-
cout<< "Error Connecting to " << servIP << endl;
-
return -1;
-
}
-
else
-
cout << "Connection Successfull to " << servIP << endl;
-
-
//Start the form off with the boundary string
-
hidden_data_len += snprintf(
-
&Buffer3[hidden_data_len],
-
sizeof(Buffer3) - hidden_data_len,
-
"%s%s\r\n",
-
"--", BOUNDARY_STRING);
-
hidden_data_len += snprintf(
-
&Buffer3[hidden_data_len],
-
sizeof(Buffer3) - (hidden_data_len),
-
"Content-Disposition: form-data;name=\"field1\"\r\n\r\n%s_auto_end\r\n",
-
syssn_no );
-
-
-
//End of multi-part form
-
hidden_data_len += snprintf(
-
&Buffer3[hidden_data_len],
-
sizeof(Buffer3) - (hidden_data_len),
-
"%s%s%s\r\n",
-
"--",BOUNDARY_STRING,"--" );
-
first_form_len += snprintf(
-
&buffer1[first_form_len],
-
sizeof(buffer1) - first_form_len,
-
"%s%s\r\n",
-
"--",BOUNDARY_STRING );
-
//Some random filename - must end in .dat because web server checks
-
//This is the data portion of the form - the flat file will be sent in this section
-
first_form_len += snprintf(
-
&buffer1[first_form_len],
-
sizeof(buffer1) - first_form_len,
-
"Content-Disposition: form-data; name=\"flat_file\"; filename=\"travellerdata.dat\"\r\n" );
-
-
first_form_len += snprintf(
-
&buffer1[first_form_len],
-
sizeof(buffer1) - first_form_len,
-
"Content-Type: text/plain\r\n\r\n" );
-
-
-
//Prepare the ending part of the form-data - this follows the flat file contents
-
//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 );*/
-
-
-
// Put together the headers for HTTP POST
-
count = snprintf(
-
&buffer[0],
-
sizeof(buffer),
-
"POST %s HTTP/1.1\r\n",
-
m_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)\r\n");
-
count += snprintf(
-
&buffer[count],
-
sizeof(buffer) - count,
-
"Host: %s\r\n",
-
m_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+hidden_data_len );
-
count += snprintf(
-
&buffer[count],
-
sizeof(buffer) - count,
-
"\r\n" );
-
//cout << buffer << endl;
-
count1 += snprintf(
-
&buffer2[count1],
-
sizeof(buffer2) - count1,
-
"%s%s\r\n",
-
"--",BOUNDARY_STRING);
-
-
count1 += snprintf(
-
&buffer2[count1],
-
sizeof(buffer2) - count1,
-
"Content-Disposition: form-data; \"\r\n");
-
count1 += snprintf(
-
&buffer2[count1],
-
sizeof(buffer2) - count1,
-
"Content-Type: text/html\r\n");
-
count1 += snprintf(
-
&buffer2[count1],
-
sizeof(buffer2) - count1,
-
"Content-Length: %d\r\n", data_len);
-
count2 += snprintf(
-
&buffer3[count2],
-
sizeof(buffer3) - (count1+count2),
-
"%s%s%s\r\n",
-
"--",BOUNDARY_STRING, "--");
-
-
string status;
-
-
cout << "Sending Post Headers" << endl<< buffer << endl << "End Post Headers" << endl;
-
-
//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;
-
}
-
-
cout << "Hidden Data" << endl<< Buffer3 << endl << "End Post Headers" << endl;
-
if (send(sock, Buffer3, hidden_data_len, 0) == -1) {
-
-
//status = perror("send");
-
cout << "Stats Worker: received socket error %d on header send\n" << endl;
-
return -1;
-
}
-
cout << "Now Sending first part of the form data BUFFER1" << endl << buffer1 << endl<< "End First Part"<< endl;
-
-
-
-
if (send(sock, buffer1, first_form_len, 0) == -1) {
-
-
//status = CK_Get_last_error();
-
cout << "Stats Worker: received socket error on header send\n" << endl;
-
return -1;
-
}
-
-
cout << "Now Sending file " << Traveller_output_buffer << endl;
-
//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;
-
}
-
cout << "Sending ending boundary string " << endl << buffer3 << endl;
-
if (send(sock, buffer3, count2, 0) == -1) {
-
-
//status = perror("send");
-
cout << "Stats Worker: received socket error %d on header 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;
-
}*/
-
-
//Receive server response
-
-
n = recv(sock, buf, 2000, 0);
-
//cout << "Server Response is " << endl << buf << endl;
-
if(n < 0)
-
{
-
cout << "Upload Failed" << endl;
-
return -1;
-
}
-
-
// Make sure this is a HTTP header
-
if (!strstr(buf, "HTTP")) {
-
-
cout << "Stats Worker: response doesn't contain HTTP\n";
-
return -1;
-
}
-
-
if (n < 0) {
-
-
cout << "Stats Worker: response status is " << n;
-
return -1;
-
}
-
-
// while (n > 0) {
-
//printf(buf);
-
if(!strstr(buf, Successful_Upload))
-
{
-
cout << "Traveller Upload Failed. Server Responded " << buf;
-
return -1;
-
}
-
else
-
{
-
cout << "Traveller Upload Successfull. \n Server Response is \n" << buf;
-
//return 0;
-
}
-
//n = recv(sock, buf, 10480, 0);
-
//}
-
//remove("newtravellerdata.dat");
-
//close(sock);
-
}
-
If required I can upload the server side cgi too.
Thanks,
Mohit
1 9790
Hi,
Can anybody help me out.
This is the whole output that I am trying to send through a post request -
POST http://192.168.2.5/upload.cgi HTTP/1.1
-
Accept-Language: en-us
-
Content-Type: multipart/form-data; boundary=---------------------------7d2b119100532
-
User-Agent: Mozilla/3.01 (compatible)
-
Host: 192.168.2.5
-
Pragma: no-cache
-
Content-Length: 717
-
-
-----------------------------7d2b119100532
-
Content-Disposition: form-data; name="field1"
-
-
5107115015_auto_end
-
-----------------------------7d2b119100532
-
Content-Disposition: form-data; name="flat_file"; filename="travellerdata.dat"
-
Content-Type: text/plain
-
-
<SYSSN>5107115015</SYSSN>
-
<HDD slot="0">
-
<serial>DA40P7C003P5</serial>
-
<model>MAW3300NC</model>
-
<firmware>0104</firmware>
-
</HDD>
-
<network>
-
<adapter slot="1">
-
<interface slot="1">
-
<mac>00:E0:81:4B:8F:FD</mac>
-
</interface>
-
</adapter>
-
</network>
-
<BIOS>
-
<revision></revision>
-
<date>06/23/06</date>
-
</BIOS>
-
<CPU_count>4</CPU_count>
-
<CPU slot="0">
-
<serial>00020F120000000000000000</serial>
-
</CPU>
-
-----------------------------7d2b119100532--
-
however I receive the following response back from the server
Server Response is
HTTP/1.1 200 OK
Date: Fri, 26 Sep 2008 05:13:55 GMT
Server: Apache/2.2.3 (Debian) PHP/5.2.0-8+etch10
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
f3
<h1>Software error:</h1>
<pre>Malforme d multipart POST
</pre>
<p>
For help, please send mail to the webmaster (<a href="mailto:we bmaster@localho st">webmaster@l ocalhost</a>), giving this error message
and the time and date of the error.
</p>
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Niels Berkers |
last post by:
Hi,
i'd like to host my web pages using multiparts to reduce the number of
hits on the server.
i know this isn't a real PHP subject, but i'll try it anyway.
i've been searching the web for solutions and examples with no succes.
does anybody know a good starting point
hints / tips are also welcome
Regards
|
by: LRW |
last post by:
Because I've had recipients of our newsletter tell us they can't (or
won't) receive HTML e-mail, I found out how to make an e-mail that
sends both HTML and a plaintext version in a multipart message.
Problem is, while the HTML version shows up fine in HTML enabled
clients like Outlook, in plaintext clients it either shows nothing in
the body or just an attachment link to the message source code.
I've tried different encodings and bits...
|
by: Damien |
last post by:
Hi to all,
After hours of attempts and "googling", I'm still pulling my hair off my
head when I try to send multipart html emails.
It "works" on PCs with Outlook when I juste send a single "related"
mail: one part for the HTML body, and several for the images. However,
the images do not show on a Mac.
I also wanted to have an "alternate", plain text message. I've tried the
method described by Zend and PHPBuilder, but no luck...
|
by: Hunter Peress |
last post by:
I have been unable to write a script that can do (and receieve) a multipart form upload.
Also, it seems that there really are differences between python's implementation and else's.
Can someone please prove me wrong with a script that works with itself AND with example 18-2
from php: http://www.php.net/manual/en/features.file-upload.php
__________________________________
|
by: Travis Pupkin |
last post by:
Hi,
I have a form that triggers the sending of an e-mail via CDOSYS. I'd
like to make this a nice HTML-formatted multipart message, but for some
reason the text version is coming through blank.
Originally I thought I had read that CDO has an automatic text converter
that will turn the HTML code into a plain TEXT message, but when that
didn't work (and I couldn't find anything further about it), I added a
custom text line like this:
| |
by: Der tolle Emil |
last post by:
Hi!
I wrote a little function to send emails which works quite well. I already
managed to send attachments correctly (also more than 1 per email) but I am
not able to send a HTML mail containing a text only block for non-HTML
clients. I will not post the PHP code as I think it is irrelevant, the error
lies within the mail header and/or body, so here is the mail I do want to
send:
FROM: me <foo@bar.com>
|
by: madmak |
last post by:
Hi, I am a noob with PHP and need some asistance regarding PHP and lotus notes. I am trying to create a multipart message in PHP to send mail via lotus notes.
Here is the code snippet.
<?php
---some code here --
$session_notes = new COM("Lotus.NotesSession");
$session_notes->Initialize("<password>");
|
by: fnoppie |
last post by:
Hi,
I am near to desperation as I have a million things to get a solution
for my problem. I have to post a multipart message to a url that
consists of a xml file and an binary file (pdf). Seperately the
posting words fine but when I want to create one multipart message
with both then things go wrong.
The binary file is converted and of datatype byte()
The xml file is just a string.
|
by: pezkel |
last post by:
Hi,
For the last two weeks I have been looking for a solid way to create an asp page that can upload a binary file and an xml file in a multipart message to a third party website.
I am on the verge of giving (and cracking) up so I would appreciate any pointers. I have used:
http://www.motobit.com/tips/detpg_uploadvbsie/
and end up with the problem that I have a byte array for the binary file and a string for the xml. I have tried...
|
by: ceh |
last post by:
Hi, on windows xp I'm using
xampp v 1.6.4
I'm trying to send mail. The mail always sends, but the multipart
sections are broken. Essentially, I want to send an html email that
has a link it, like http://www.google.com so the reader can click on
the link.
When I get the email, the body is empty.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |