├── README.md ├── HttpRequest.py ├── HttpRequest.js ├── HttpRequest.cs └── HttpRequest.java /README.md: -------------------------------------------------------------------------------- 1 | # HttpRequests 2 | HttpRequests in all programming languages that I know. 3 | 4 | Each code has a GET function, and a POST function with values. 5 | In addition, each code contains a secondary function to execute the request: "ExecuteRequest" with Request object and returns a response string. 6 | -------------------------------------------------------------------------------- /HttpRequest.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | def GET(requestURL): 4 | return requests.get(requestURL).text 5 | 6 | def POST(requestURL, data): 7 | return requests.post(requestURL,data).text 8 | 9 | print("GET:https://httpbin.org/ip") 10 | print(GET("https://httpbin.org/ip")) 11 | 12 | print("POST:https://httpbin.org/post\tDATA:\"Hello World\"") 13 | print(POST("https://httpbin.org/post", "Hello World")) -------------------------------------------------------------------------------- /HttpRequest.js: -------------------------------------------------------------------------------- 1 | function GET(requestURL) 2 | { 3 | var request = new XMLHttpRequest(); 4 | request.open("GET",requestURL,true); 5 | request.onreadystatechange = function(){ 6 | if(request.readyState == 4){ 7 | document.write(request.responseText + "
"); 8 | } 9 | } 10 | request.send(null); 11 | } 12 | 13 | function POST(requestURL, data) 14 | { 15 | var request = new XMLHttpRequest(); 16 | request.open("POST",requestURL,true); 17 | request.onreadystatechange = function(){ 18 | if(request.readyState == 4){ 19 | document.write(request.responseText + "
"); 20 | } 21 | } 22 | request.send("data=" + data); 23 | } 24 | 25 | window.onload = function(){ 26 | document.write("GET:https://httpbin.org/ip
"); 27 | GET("https://httpbin.org/ip"); 28 | 29 | document.write("POST:https://httpbin.org/post DATA:\"Hello World\"
"); 30 | POST("https://httpbin.org/post", "Hello World"); 31 | } -------------------------------------------------------------------------------- /HttpRequest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Net; 4 | using System.Threading.Tasks; 5 | 6 | namespace HttpRequest 7 | { 8 | class Program 9 | { 10 | public static string ExecuteRequest(HttpWebRequest httpRequest) 11 | { 12 | HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponseAsync().Result; //this is Task<> 13 | StreamReader streamReader = new StreamReader(response.GetResponseStream()); 14 | return streamReader.ReadToEnd(); 15 | } 16 | 17 | public static string GET(string url) 18 | { 19 | HttpWebRequest request = WebRequest.CreateHttp(url); 20 | return ExecuteRequest(request); 21 | } 22 | 23 | public static string POST(string url, string payload) 24 | { 25 | payload = "data=" + payload; 26 | HttpWebRequest request = WebRequest.CreateHttp(url); 27 | request.Method = "POST"; 28 | request.ContentType = "application/x-www-form-urlencoded"; 29 | 30 | 31 | StreamWriter streamWriter = new StreamWriter(request.GetRequestStreamAsync().Result); 32 | streamWriter.Write(payload); 33 | streamWriter.Flush(); 34 | 35 | return ExecuteRequest(request); 36 | } 37 | 38 | static void Main(string[] args) 39 | { 40 | Console.WriteLine("GET:https://httpbin.org/ip"); 41 | Console.WriteLine(GET("https://httpbin.org/ip")); 42 | 43 | Console.WriteLine("POST:https://httpbin.org/post\tDATA:\"Hello World\""); 44 | Console.WriteLine(POST("https://httpbin.org/post", "Hello World")); 45 | 46 | Console.Read(); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /HttpRequest.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.DataOutputStream; 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.InputStreamReader; 6 | import java.net.HttpURLConnection; 7 | import java.net.MalformedURLException; 8 | import java.net.ProtocolException; 9 | import java.net.URL; 10 | 11 | public class HttpRequest { 12 | 13 | public static String ExecuteRequest(HttpURLConnection httpRequest) throws IOException { 14 | 15 | // execute the request and get the response to InputStream 16 | InputStream inputStream = httpRequest.getInputStream(); // throws 17 | // IOException 18 | 19 | // Read from the InputStream 20 | BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 21 | 22 | // Build String from reader 23 | StringBuffer response = new StringBuffer(); 24 | 25 | String line; 26 | while ((line = reader.readLine()) != null) { 27 | response.append(line); 28 | } 29 | reader.close(); 30 | 31 | return response.toString(); 32 | } 33 | 34 | public static String GET(String httpURL) { 35 | 36 | try { 37 | URL url = new URL(httpURL); // throw MalformedURLException 38 | 39 | HttpURLConnection httpRequest = (HttpURLConnection) url.openConnection(); 40 | httpRequest.setRequestMethod("GET"); // throw ProtocolException 41 | 42 | return ExecuteRequest(httpRequest); 43 | 44 | } catch (MalformedURLException e) { 45 | e.printStackTrace(); 46 | return e.getMessage(); 47 | 48 | } catch (ProtocolException e) { 49 | e.printStackTrace(); 50 | return e.getMessage(); 51 | 52 | } catch (IOException e) { 53 | e.printStackTrace(); 54 | return e.getMessage(); 55 | } 56 | } 57 | 58 | public static String POST(String httpURL, String payload){ 59 | try { 60 | payload="payload="+payload; 61 | URL url = new URL(httpURL); // throw MalformedURLException 62 | 63 | HttpURLConnection httpRequest = (HttpURLConnection) url.openConnection(); 64 | httpRequest.setRequestMethod("POST"); // throw ProtocolException 65 | 66 | //Http request Headers 67 | httpRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 68 | httpRequest.setRequestProperty("Content-Length", Integer.toString(payload.getBytes().length)); 69 | httpRequest.setDoOutput(true); //use the URL Connection for input 70 | 71 | DataOutputStream outputStream = new DataOutputStream(httpRequest.getOutputStream()); 72 | outputStream.writeBytes(payload); 73 | outputStream.close(); 74 | 75 | return ExecuteRequest(httpRequest); 76 | 77 | } catch (MalformedURLException e) { 78 | e.printStackTrace(); 79 | return e.getMessage(); 80 | 81 | } catch (ProtocolException e) { 82 | e.printStackTrace(); 83 | return e.getMessage(); 84 | 85 | } catch (IOException e) { 86 | e.printStackTrace(); 87 | return e.getMessage(); 88 | } 89 | } 90 | public static void main(String args[]) { 91 | 92 | System.out.println("GET:http://httpbin.org/ip"); 93 | System.out.println(GET("http://httpbin.org/ip")); 94 | 95 | System.out.println("POST:http://httpbin.org/post"); 96 | System.out.println(POST("http://httpbin.org/post","Hello World")); 97 | } 98 | } 99 | --------------------------------------------------------------------------------