Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides
https://forum.ivorde.com/

Using curl to get the HTTP response from an HTTP server
https://forum.ivorde.com/using-curl-to-get-the-http-response-from-an-http-server-t97.html
Page 1 of 1

Author:  debuser [ Sun Jun 06, 2010 9:05 am ]
Post subject:  Using curl to get the HTTP response from an HTTP server

Using curl to get the HTTP response from an HTTP server.

Syntax: curl --head http://www.site.name

Quote:
curl(1) Curl Manual curl(1)

NAME
curl - transfer a URL

SYNOPSIS
curl [options] [URL...]

DESCRIPTION
curl is a tool to transfer data from or to a server, using one of the
supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT,
TELNET, LDAP or FILE). The command is designed to work without user
interaction.

curl offers a busload of useful tricks like proxy support, user authen-
tication, ftp upload, HTTP post, SSL connections, cookies, file trans-
fer resume and more. As you will see below, the amount of features will
make your head spin!

curl is powered by libcurl for all transfer-related features. See
libcurl(3) for details.

-I/--head
(HTTP/FTP/FILE) Fetch the HTTP-header only! HTTP-servers feature
the command HEAD which this uses to get nothing but the header
of a document. When used on a FTP or FILE file, curl displays
the file size and last modification time only.

If this option is used twice, the second will again disable
header only.


Code:
$ curl --head http://www.google.cz
HTTP/1.1 200 OK
Date: Sun, 06 Jun 2010 13:02:15 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-2
Set-Cookie: PREF=ID=bd077f51a60704c8:TM=1275829335:LM=1275829335:S=P9IvQj_4SmpaSGqp; expires=Tue, 05-Jun-2012 13:02:15 GMT; path=/; domain=.google.cz
Server: gws
X-XSS-Protection: 1; mode=block
Transfer-Encoding: chunked

Author:  wnbv85 [ Thu Dec 01, 2011 8:07 am ]
Post subject:  Re: Using curl to get the HTTP response from an HTTP server

hi
function http_response($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

echo $httpCode ;
}

I am developing a software which will be communicating with a server using HTTP (but it is not a web browser). Since the server part is not ready yet, I would like to debug my client software by sending HTTP messages to it. I know that I can send HTTP requests using Curl. But I am not sure if that is sufficient here.

I'm imagining an environment where I send a request from my application, check that it is correct using Wireshark and then reply to the request using some software. Using Curl, I think I would have open a listening port..?

Author:  admin [ Tue Dec 06, 2011 8:45 am ]
Post subject:  Re: Using curl to get the HTTP response from an HTTP server

You can use netcat utility to open an http port:
Code:
# nc -v -l 80 &
# lsof -Pni :80
COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME
nc      1466 root    3u  IPv4   6349       TCP *:80 (LISTEN)



First command runs nc with "verbose" and "listen to port 80" instructions in background mode.

From the same terminal you can send some text to port 80 on localhost (where nc is listening as server) and it will output it to the standard output:
Code:
# echo 'test output' | nc localhost 80
Connection from 127.0.0.1 port 80 [tcp/http] accepted
test output

Author:  admin [ Tue Dec 06, 2011 9:05 am ]
Post subject:  Re: Using curl to get the HTTP response from an HTTP server

Server:
Code:
# echo 'HTTP/1.1 OK' | nc -l 80
asdf


Client:
Code:
# echo asdf | nc localhost 80
HTTP/1.1 OK

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/