socket

T

Teo81

Hi. I need to send an HTTP post specifying password and username to a
web server through a socket in order to login. I'm writing this
program in c under a linux system. How does the request has to be
formatted? I send the request using the following code:

write( socket_fd, buffer, strlen( buffer ));

where socket_fd is the socket descriptor, buffer is the string
containing the post request and strlen(buffer) its length.

thank you
 
M

Michael Mair

Teo81 said:
Hi. I need to send an HTTP post specifying password and username to a
web server through a socket in order to login. I'm writing this
program in c under a linux system. How does the request has to be
formatted? I send the request using the following code:

write( socket_fd, buffer, strlen( buffer ));

where socket_fd is the socket descriptor, buffer is the string
containing the post request and strlen(buffer) its length.

comp.lang.c is not the right place for this question[*]; I'd try
comp.unix.programmer.


Cheers
Michael

[*] Have a look at http://clc-wiki.net 's introduction to
comp.lang.c
 
J

Joe Estock

Teo81 said:
Hi. I need to send an HTTP post specifying password and username to a
web server through a socket in order to login. I'm writing this
program in c under a linux system. How does the request has to be
formatted? I send the request using the following code:

write( socket_fd, buffer, strlen( buffer ));

where socket_fd is the socket descriptor, buffer is the string
containing the post request and strlen(buffer) its length.

thank you

As previously stated by Michael Mair this is not the correct place to
ask this question, however I am unaware of the correct newsgroup to
direct you to.

Since by chance I have previously done what you are attempting to do,
here is my two cents. If you need any additional information either find
the relevant newsgroup or try searching google for "http 1.1 rfc" (or
http 1.0 depending on your needs):

<OT>
Authorization is the http header you are needing. The format is as follows:

Authorization: cxHvb57

In the above example, Authorization is the header you need to send, : is
the delimiter used to separate the header name from the value, and
cxHvb57 is a base64 encoded string in the form "username:password". For
example (in pseudo-code) :

size_t http_base64_encode(char *username, char *password, char *tmp)
{
sprintf(tmp, "%s:%s", username, password);

/* do your encoding on tmp here */

return(strlen(tmp));
}

int main(void)
{
char temp[1024];
char stub[200];

http_base64_encode("foo", "bar", stub);

sprintf(temp, "Authorization: %s", stub);

write(socket_fd, temp, strlen(temp));
}
</OT>
 
K

kondal

Teo81 said:
Hi. I need to send an HTTP post specifying password and username to a
web server through a socket in order to login. I'm writing this
program in c under a linux system. How does the request has to be
formatted? I send the request using the following code:

write( socket_fd, buffer, strlen( buffer ));

where socket_fd is the socket descriptor, buffer is the string
containing the post request and strlen(buffer) its length.

thank you

Read HTTP protocol RFC.

The post data would be in the HTTP protocol data part. So you need to
send the HTTP header first then the data.

POST /login.cgi http/1.1\r\n
\r\n\r\n
username=user&password=pass

ps. each line in protocol headers ends with CRLF and protocol headers
end with two CRLF

for quick understanding
http://www.jmarshall.com/easy/http/
http://en.wikipedia.org/wiki/HTTP

-kondal
 
M

Mark McIntyre

As previously stated by Michael Mair this is not the correct place to
ask this question, however I am unaware of the correct newsgroup to
direct you to.

A unix programming one.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
J

Joe Estock

Mark said:
A unix programming one.

I'm not so sure this is a unix-specific question. The OP seems to
understand the code and the logic just fine, hence my response. If
anything this was more of a protocol issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top