add httpServer
This commit is contained in:
		
							parent
							
								
									6b3f8c06c3
								
							
						
					
					
						commit
						2ba2a07d64
					
				| @ -1,8 +1,33 @@ | ||||
| package sh.adb.RandomRedditMemesAPI; | ||||
| 
 | ||||
| import com.sun.net.httpserver.HttpExchange; | ||||
| import com.sun.net.httpserver.HttpHandler; | ||||
| import com.sun.net.httpserver.HttpServer; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.io.OutputStream; | ||||
| import java.net.InetSocketAddress; | ||||
| 
 | ||||
| public class Main { | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
| 	// write your code here | ||||
|     public static void main(String[] args) throws IOException { | ||||
|         HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); | ||||
|         server.createContext("/api/v1/", new MyHandler()); | ||||
|         server.setExecutor(null); | ||||
|         server.start(); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| class MyHandler implements HttpHandler { | ||||
|     @Override | ||||
|     public void handle(HttpExchange t) throws IOException { | ||||
|         InputStream is = t.getRequestBody(); | ||||
|         //is.read(); | ||||
|         String response = "This is the response"; | ||||
|         t.sendResponseHeaders(200, response.length()); | ||||
|         OutputStream os = t.getResponseBody(); | ||||
|         os.write(response.getBytes()); | ||||
|         os.close(); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user