C#
|
The following C# class sends web requests to the httpq server and returns the response.
|
/*
* HttpQSample.cs
*
* author: Kosta Arvanitis
* email: karvanitis@hotmail.com
* usage: httpqsample [command] [arg]
*/
using System;
using System.Net;
using System.IO;
using System.Text;
namespace HttpQSample
{
/// <summary>
/// Sends web requests to the httpq server and returns the response.
/// </summary>
class HttpQ
{
string _hostname;
string _password;
long _port;
/// <summary>
/// Initializes a new instsance of the HttpQ class for a specified server.
/// </summary>
/// <param name="hostname">The hostname/address of the httpQ server.</param>
/// <param name="password">The httpQ password.</param>
/// <param name="port">The port on which the httpQ server is running.</param>
public HttpQ(string hostname, string password, long port)
{
_hostname = hostname;
_password = password;
_port = port;
}
/// <summary>
/// Send a command to the httpQ server with no arguments.
/// </summary>
/// <param name="command">The httpQ command to send.</param>
/// <returns>The response from the server.</returns>
public HttpWebResponse SendCommand(string command)
{
return SendCommand(command, String.Empty);
}
/// <summary>
/// Send a command to the httpQ server with specified arguments.
/// </summary>
/// <param name="command">The httpQ command to send.</param>
/// <param name="args">The httpQ arguments to send.</param>
/// <returns>The response from the server.</returns>
public HttpWebResponse SendCommand(string command, string args)
{
string url = String.Format(@"http://{0}:{1}/{2}?p={3}",
_hostname, _port, command, _password);
if (args != String.Empty)
url = String.Format("{0}&a={1}", url, args);
HttpWebRequest httpWebRequest =
(HttpWebRequest)WebRequest.Create(url);
HttpWebResponse httpWebResponse =
(HttpWebResponse)httpWebRequest.GetResponse();
return httpWebResponse;
}
/// <summary>
/// Sample main.
/// </summary>
/// <example>
/// httpqsample getplaylisttitle
/// httpqsample getplaylisttitle 1
/// </example>
static void Main(string[] args)
{
HttpQ httpq = new HttpQ("localhost", "pass", 4800);
try
{
HttpWebResponse webResponse = null;
if (args.Length == 1)
webResponse = httpq.SendCommand(args[0]);
else
webResponse = httpq.SendCommand(args[0], args[1]);
Stream responseStream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
string data = streamReader.ReadToEnd();
Console.WriteLine(data);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
|
Php
|
The following Php function takes a command and arguement, sends a valid httpQ request to the server and displays the return data.
|
function httpQ($command, $arg)
{
$pass = "pass";
$port = 4800;
$fp = fsockopen("localhost", $port, &$errno, &$errstr);
if(!$fp)
{
echo "$errstr ($errno)\n";
}
else
{
if($arg == "")
{
$msg = "GET /$command?p=$pass HTTP/1.0\r\n\r\n";
}
else
{
$msg = "GET /$command?p=$pass&a=$arg HTTP/1.0\r\n\r\n";
}
fputs($fp, $msg);
while(!feof($fp))
{
echo fgets($fp,128);
}
}
fclose($fp);
}
|
Perl
|
The following Perl code sends character data wrapped with the most basic http headers to an open socket and port, reads the return data and displays it to stdout.
|
#
# http.pl
#
# Author: Kosta Arvanitis
# email: karvanitis@hotmail.com
# example: http.pl localhost 80 "/play?p=pass"
#
#!/public/bin/perl
use Socket;
if($#ARGV < 2)
{
print("Usage: http.pl 'host' 'port' 'url-string'\n");
exit(-1);
}
$remote = $ARGV[0];
$port = $ARGV[1];
$urlstr = $ARGV[2];
$myaddr = "localhost";
$iaddr = inet_aton($remote) or die "Error: $!";
$paddr = sockaddr_in($port, $iaddr) or die "Error: $!";
$proto = getprotobyname('tcp') or die "Error: $!";
socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die "Error: $!";
print "\nConnecting...$remote:$port\n";
connect(SOCK, $paddr) or die "Error: $!";
$msg = "GET $urlstr HTTP/1.0\r\n\r\n";
print "\nSending... $msg";
send(SOCK, $msg, 0) or die "Error: $!";
sleep(1);
@lines = <SOCK>;
print @lines;
print "\n\nShutting Down...\n";
close(SOCK);
|
Java
|
The folowing Java code opens a connection to a socket and sends data accross it.
|
/**
* httpq.java
*
* Author: Kosta Arvanitis
* email: karvanitis@hotmail.com
* example: java httpq localhost 4800 "/"
*/
import java.net.*;
import java.io.*;
public class httpq
{
public static void send(String host, int port, String urlstring)
{
try
{
Socket socket = new Socket(host, port);
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
DataOutputStream writer = new DataOutputStream(
socket.getOutputStream());
String msg = "GET " + urlstring + " HTTP/1.0\r\n\r\n";
writer.writeBytes(msg);
writer.flush();
String ret;
while ((ret = reader.readLine()) != null)
System.out.println(ret);
writer.close();
reader.close();
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
try
{
httpq h = new httpq();
h.send(args[0], Integer.parseInt(args[1]), args[2]);
}
catch(Exception e)
{
System.out.println("Usage: java http [host] [port] [url-string]");
}
}
}
|
|