1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* This program is a very simple Web server. When it receives a HTTP request it sends a simple OK response.
*/
public class HttpListener {
public static void main(String args[]) {
try {
// Get the port to listen on
int port = Integer.parseInt(args[0]);
// Create a ServerSocket to listen on that port.
ServerSocket ss = new ServerSocket(port);
// Now enter an infinite loop, waiting for & handling connections.
for (;;) {
// Wait for a client to connect. The method will block;
// when it returns the socket will be connected to the client
Socket client = ss.accept();
// Get input and output streams to talk to the client
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream());
// Start sending our reply, using the HTTP 1.1 protocol
out.print("HTTP/1.1 200 \r\n");
out.print("Content-Type: text/plain\r\n");
out.print("Connection: close\r\n");
out.print("\r\n");
out.print("OK");
// Now, read the HTTP request from the client. When we see the empty line, we stop reading.
String line;
while ((line = in.readLine()) != null) {
if (line.length() == 0)
break;
// do something with line data here
}
// Flush and close the output stream
out.close();
// Close the input stream
in.close();
// Close the socket itself
client.close();
}
}
// If anything goes wrong, print an error message
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java HttpListener <port>");
}
}
}
1
2
3
4
5
6
<?php
foreach ($_POST as $key => $value)
{
echo $key . '=' . var_dump($value) . '<br />';
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Net;
// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine ("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
// URI prefixes are required,
// for example "http://yourwebsite.com:8080/index".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");
// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
listener.Start();
Console.WriteLine("Listening...");
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "<HTML><BODY>OK</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer,0,buffer.Length);
// You must close the output stream.
output.Close();
listener.Stop();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cpprest\http_listener.h>
using namespace web::http::experimental::listener;
using namespace web::http;
using namespace web;
class MyListener
{
public:
MyListener(const http::uri& url);
private:
void handle_post(http_request request);
http_listener m_listener;
};
MyListener::MyListener(const http::uri& url) : m_listener(http_listener(url))
{
m_listener.support(methods::POST, std::tr1::bind(&MyListener::handle_post, this, std::tr1::placeholders::_1));
};
void MyListener::handle_post(http_request message)
{
message.reply(status_codes::OK, U("Hello"));
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Imports System.Net
Imports System.Globalization
Module HttpListener
Sub Main()
Dim prefixes(0) As String
prefixes(0) = "http://*:8080/HttpListener/"
ProcessRequests(prefixes)
End Sub
Private Sub ProcessRequests(ByVal prefixes() As String)
If Not System.Net.HttpListener.IsSupported Then
Console.WriteLine( _
"Windows XP SP2, Server 2003, or higher is required to use the HttpListener class.")
Exit Sub
End If
' URI prefixes are required,
If prefixes Is Nothing OrElse prefixes.Length = 0 Then
Throw New ArgumentException("prefixes")
End If
' Create a listener and add the prefixes.
Dim listener As System.Net.HttpListener = _
New System.Net.HttpListener()
For Each s As String In prefixes
listener.Prefixes.Add(s)
Next
Try
' Start the listener to begin listening for requests.
listener.Start()
Console.WriteLine("Listening...")
1
2
3
4
5
6
7
<%
For Each item In Request.Form
Response.Write "Key: " & item & " - Value: " & Request.Form(item) & "<BR />"
Next
%>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Get sockets from stdlib
require 'socket'
# Socket to listen on port 8080
server = TCPServer.open(8080)
# Servers run forever
loop {
# Wait for a client to connect
client = server.accept
# In this case, method = "POST" and path = "/"
method, path = client.gets.split
headers = {}
# Collect HTTP headers
while line = client.gets.split(' ', 2)
# Blank line means no more headers
break if line[0] == ""
# Hash headers by type
headers[line[0].chop] = line[1].strip
end
# Read the POST data as specified in the header
data = client.read(headers["Content-Length"].to_i)
# Do what you want with the POST data
puts data
# Send response to the client
client.puts "OK"
# Disconnect from the client
client.close
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_POST(self):
# Doesn't do anything with posted data
content_length = int(self.headers['Content-Length']) # Gets the size of data
post_data = self.rfile.read(content_length) # Gets the data itself
self._set_headers()
self.wfile.write("<html><body>OK</body></html>")
def run(server_class=HTTPServer, handler_class=S, port=80):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Starting httpd...'
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()