Kurse:HTTP Code Übungen: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
| Zeile 1: | Zeile 1: | ||
= HTTP | == HTTP Spickzettel (MediaWiki) == | ||
== | === HTTP-Protokoll – Übersicht === | ||
* HTTP ist ein zustandsloses Protokoll | * HTTP (Hypertext Transfer Protocol) ist ein zustandsloses Protokoll ... | ||
... | |||
== | === Übung: Statuscode und Header setzen === | ||
<pre> | |||
< | |||
var builder = WebApplication.CreateBuilder(args); | var builder = WebApplication.CreateBuilder(args); | ||
var app = builder.Build(); | var app = builder.Build(); | ||
| Zeile 92: | Zeile 12: | ||
app.Run(async (HttpContext context) => | app.Run(async (HttpContext context) => | ||
{ | { | ||
context.Response. | context.Response.StatusCode = 200; | ||
context.Response.Headers["Content-Type"] = "text/html"; | context.Response.Headers["Content-Type"] = "text/html"; | ||
await context.Response.WriteAsync(" | await context.Response.WriteAsync("<h1>Übung erfolgreich</h1>"); | ||
}); | }); | ||
app.Run(); | app.Run(); | ||
</ | </pre> | ||
'''Ziel:''' Einen eigenen HTTP-Header setzen und eine HTML-Antwort senden. | |||
=== | === Übung: Abfrage von Query-Parametern === | ||
< | <pre> | ||
app.Run(async (HttpContext context) => | app.Run(async (HttpContext context) => | ||
{ | { | ||
if (context.Request.Method == "GET" && context.Request.Query.ContainsKey("name")) | |||
{ | |||
string name = context.Request.Query["name"]; | |||
await context.Response.WriteAsync($"<p>Hallo {name}!</p>"); | |||
} | |||
else | |||
{ | |||
await context.Response.WriteAsync("<p>Bitte gib einen Namen per Query an (z.B. ?name=Max)</p>"); | |||
} | |||
== | |||
{ | |||
await context.Response.WriteAsync( | |||
}); | }); | ||
</ | </pre> | ||
'''Ziel:''' Umgang mit Query-Strings (z. B. ?name=Max) trainieren. | |||
=== | === Übung: User-Agent anzeigen === | ||
< | <pre> | ||
app.Run(async (HttpContext context) => | app.Run(async (HttpContext context) => | ||
{ | { | ||
string agent = context.Request.Headers["User-Agent"]; | |||
await context.Response.WriteAsync($"<p>Dein Browser: {agent}</p>"); | |||
}); | }); | ||
</ | </pre> | ||
'''Ziel:''' HTTP-Header auslesen und anzeigen. | |||
=== | === Übung: HTTP-Request analysieren === | ||
< | <pre> | ||
app.Run(async (HttpContext context) => | app.Run(async (HttpContext context) => | ||
{ | { | ||
var path = context.Request.Path; | |||
var method = context.Request.Method; | |||
await context.Response.WriteAsync($"<p>Pfad: {path}</p>"); | |||
await context.Response.WriteAsync($"<p>Methode: {method}</p>"); | |||
} | |||
}); | }); | ||
</ | </pre> | ||
'''Ziel:''' Grundlagen zu Methode und Pfad aus Requests üben. | |||
' | |||
''' | |||
''' | === Übung: GET vs. POST Test mit Postman === | ||
# | '''Anleitung:''' | ||
# | # Starte deine ASP.NET Core App lokal. | ||
# | # Öffne Postman. | ||
# Sende GET- und POST-Anfragen an http://localhost:7070 mit passenden Inhalten. | |||
# Beobachte Verhalten bei: | |||
* GET mit Query | |||
* POST mit Body (JSON oder Form-Daten) | |||
'''Ziel:''' Unterschiede zwischen GET- und POST-Anfragen erfahren und testen. | |||
Aktuelle Version vom 23. Juni 2025, 21:19 Uhr
HTTP Spickzettel (MediaWiki)
HTTP-Protokoll – Übersicht
- HTTP (Hypertext Transfer Protocol) ist ein zustandsloses Protokoll ...
...
Übung: Statuscode und Header setzen
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Run(async (HttpContext context) =>
{
context.Response.StatusCode = 200;
context.Response.Headers["Content-Type"] = "text/html";
await context.Response.WriteAsync("<h1>Übung erfolgreich</h1>");
});
app.Run();
Ziel: Einen eigenen HTTP-Header setzen und eine HTML-Antwort senden.
Übung: Abfrage von Query-Parametern
app.Run(async (HttpContext context) =>
{
if (context.Request.Method == "GET" && context.Request.Query.ContainsKey("name"))
{
string name = context.Request.Query["name"];
await context.Response.WriteAsync($"<p>Hallo {name}!</p>");
}
else
{
await context.Response.WriteAsync("<p>Bitte gib einen Namen per Query an (z.B. ?name=Max)</p>");
}
});
Ziel: Umgang mit Query-Strings (z. B. ?name=Max) trainieren.
Übung: User-Agent anzeigen
app.Run(async (HttpContext context) =>
{
string agent = context.Request.Headers["User-Agent"];
await context.Response.WriteAsync($"<p>Dein Browser: {agent}</p>");
});
Ziel: HTTP-Header auslesen und anzeigen.
Übung: HTTP-Request analysieren
app.Run(async (HttpContext context) =>
{
var path = context.Request.Path;
var method = context.Request.Method;
await context.Response.WriteAsync($"<p>Pfad: {path}</p>");
await context.Response.WriteAsync($"<p>Methode: {method}</p>");
});
Ziel: Grundlagen zu Methode und Pfad aus Requests üben.
Übung: GET vs. POST Test mit Postman
Anleitung:
- Starte deine ASP.NET Core App lokal.
- Öffne Postman.
- Sende GET- und POST-Anfragen an http://localhost:7070 mit passenden Inhalten.
- Beobachte Verhalten bei:
* GET mit Query * POST mit Body (JSON oder Form-Daten)
Ziel: Unterschiede zwischen GET- und POST-Anfragen erfahren und testen.