Kurse:HTTP Code Übungen: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Die Seite wurde neu angelegt: „== Übungen zu HTTP-Protokoll und ASP.NET Core == === Übung 1: HTTP-Antwort beobachten im Browser === # Öffne deine Beispiel-Webseite in Chrome oder Firefox. # Aktiviere die Entwickler-Tools und wechsle zum Netzwerk-Tab (siehe §19.6 in LaunchCode) :contentReference[oaicite:1]{index=1}. # Lade die Seite neu und identifiziere: : * Eine GET‑Anfrage für das HTML-Dokument : * Eine GET‑Anfrage für eine CSS-Datei : * Eine GET‑Anfrage für ein Bild #…“ |
Keine Bearbeitungszusammenfassung |
||
| Zeile 1: | Zeile 1: | ||
= | = HTTP-Protokoll (Hypertext Transfer Protocol) = | ||
== | == Überblick == | ||
* HTTP ist ein zustandsloses Protokoll zur Übertragung von Hypertext (z. B. HTML) im Internet. | |||
* Client-Server-Modell: Der Client (z. B. Browser) sendet Anfragen, der Server antwortet. | |||
* Jede Anfrage ist unabhängig von vorherigen (stateless). | |||
: | |||
== | == Anfragemodell == | ||
* '''Client Request''': Der Client sendet eine HTTP-Anfrage. | |||
* '''Server Response''': Der Server verarbeitet die Anfrage und antwortet mit Ressourcen oder Fehlermeldungen. | |||
== HTTP-Server == | |||
HTTP | '''Definition''': Software, die HTTP-Anfragen verarbeitet und Antworten liefert. | ||
Server | |||
'''Beispiele''': | |||
* Apache HTTP Server | |||
* Nginx | |||
* Microsoft IIS | |||
* Kestrel (ASP.NET Core) | |||
( | |||
'''Kestrel''': | |||
* Cross-Platform Webserver in ASP.NET Core | |||
* | * Leichtgewichtig und leistungsfähig | ||
* | |||
< | == Ablauf von Anfrage und Antwort mit Kestrel == | ||
# Client sendet Anfrage | |||
# Kestrel empfängt und leitet sie an Middleware weiter | |||
# Middleware verarbeitet Anfrage | |||
# Anwendung erzeugt Antwort | |||
# Kestrel sendet Antwort zurück an den Client | |||
== Browser & HTTP == | |||
* Browser fordern HTML, CSS, JS, Bilder über HTTP an | |||
* Klick auf Link → HTTP-Request → Server → Response → Darstellung im Browser | |||
== HTTP-Anfragen in Chrome Dev Tools beobachten == | |||
* Öffnen: F12 oder Ctrl+Shift+I (Cmd+Option+I auf Mac) | |||
* Reiter „Network“ → Details zu: | |||
** Headers | |||
** Response | |||
** Timing | |||
== Aufbau einer HTTP-Antwort == | |||
'''Format''': | |||
HTTP/1.1 200 OK | |||
Content-Type: text/html | |||
Content-Length: 137 | |||
<html> | |||
<body> | |||
<h1>Hello, World!</h1> | |||
</body> | |||
</html> | |||
'''Wichtige Header''': | |||
* Content-Type: Typ der Ressource (z. B. text/html) | |||
* Content-Length: Größe der Antwort | |||
* Server: Infos zum Server | |||
* Set-Cookie: Setzt Cookies im Client | |||
* Cache-Control: Caching-Strategie | |||
== Kestrel Standard-Header == | |||
* Content-Type: z. B. text/html, application/json | |||
* Server: „Kestrel“ | |||
* Date: Zeitstempel der Antwort | |||
== HTTP-Statuscodes == | |||
=== Kategorien === | |||
* '''1xx''': Information | |||
* '''2xx''': Erfolg | |||
* '''3xx''': Weiterleitung | |||
* '''4xx''': Clientfehler | |||
* '''5xx''': Serverfehler | |||
=== Gängige Codes === | |||
* 200 OK | |||
* 201 Created | |||
* 204 No Content | |||
* 400 Bad Request | |||
* 401 Unauthorized | |||
* 403 Forbidden | |||
* 404 Not Found | |||
* 500 Internal Server Error | |||
* 503 Service Unavailable | |||
== ASP.NET Core Codebeispiele == | |||
=== Beispiel 1: Antwort-Header setzen === | |||
<syntaxhighlight lang="csharp"> | |||
var builder = WebApplication.CreateBuilder(args); | var builder = WebApplication.CreateBuilder(args); | ||
var app = builder.Build(); | var app = builder.Build(); | ||
app.Run(async context => { | app.Run(async (HttpContext context) => | ||
context.Response.Headers["Content-Type"] = "text/ | { | ||
await context.Response.WriteAsync("Hello | context.Response.Headers["MyKey"] = "my value"; | ||
context.Response.Headers["Server"] = "My server"; | |||
context.Response.Headers["Content-Type"] = "text/html"; | |||
await context.Response.WriteAsync("<h1>Hello</h1>"); | |||
await context.Response.WriteAsync("<h2>World</h2>"); | |||
}); | }); | ||
app.Run(); | app.Run(); | ||
</ | </syntaxhighlight> | ||
=== Beispiel 2: Statuscode setzen === | |||
<syntaxhighlight lang="csharp"> | |||
app.Run(async (HttpContext context) => | |||
{ | |||
context.Response.StatusCode = 200; | |||
await context.Response.WriteAsync("Hello"); | |||
await context.Response.WriteAsync(" World"); | |||
}); | |||
</syntaxhighlight> | |||
== HTTP-Request-Aufbau == | |||
* '''Startzeile''': Methode, URI, HTTP-Version | |||
* '''Header''': z. B. User-Agent, Accept, Authorization | |||
* '''Leerzeile''' | |||
* '''Body (optional)''': z. B. JSON, Formulardaten | |||
=== Beispiel: GET-Anfrage === | |||
GET /products?category=electronics&brand=apple HTTP/1.1 | |||
Host: example.com | |||
=== Beispiel: POST-Anfrage === | |||
POST /login HTTP/1.1 | |||
Host: example.com | |||
Content-Type: application/x-www-form-urlencoded | |||
username=john&password=secret | |||
== Query Strings == | |||
* Parameter in URL übergeben | |||
* Beispiel: | |||
https://example.com/products?category=electronics&brand=apple | |||
== Zugriff auf Request-Details in ASP.NET Core == | |||
=== Beispiel: Pfad und Methode auslesen === | |||
<syntaxhighlight lang="csharp"> | |||
app.Run(async (HttpContext context) => | |||
{ | |||
string path = context.Request.Path; | |||
string method = context.Request.Method; | |||
context.Response.Headers["Content-type"] = "text/html"; | |||
await context.Response.WriteAsync($"<p>{path}</p>"); | |||
await context.Response.WriteAsync($"<p>{method}</p>"); | |||
}); | |||
</syntaxhighlight> | |||
=== Beispiel: Query-Parameter auslesen === | |||
<syntaxhighlight lang="csharp"> | |||
app.Run(async (HttpContext context) => | |||
{ | |||
if (context.Request.Method == "GET" && | |||
context.Request.Query.ContainsKey("id")) | |||
{ | |||
string id = context.Request.Query["id"]; | |||
await context.Response.WriteAsync($"<p>ID: {id}</p>"); | |||
} | |||
}); | |||
</syntaxhighlight> | |||
=== Beispiel: User-Agent anzeigen === | |||
<syntaxhighlight lang="csharp"> | |||
app.Run(async (HttpContext context) => | |||
{ | |||
if (context.Request.Headers.ContainsKey("User-Agent")) | |||
{ | |||
string agent = context.Request.Headers["User-Agent"]; | |||
await context.Response.WriteAsync($"<p>{agent}</p>"); | |||
} | |||
}); | |||
</syntaxhighlight> | |||
== | == HTTP-Methoden == | ||
* '''GET''': Daten abfragen, sichtbar in URL, cachebar, sicher | |||
* | * '''POST''': Daten senden (z. B. Formulare), im Body, nicht idempotent | ||
=== | == GET vs POST == | ||
'''GET''': | |||
* Daten in URL | |||
* cachebar, idempotent | |||
* nicht für sensible Daten | |||
'''POST''': | |||
* | * Daten im Body | ||
* sicherer für Passwörter | |||
* nicht idempotent | |||
== Tool: Postman == | |||
'''Verwendung''': | |||
* HTTP-Requests (GET, POST, usw.) erstellen | |||
* Header & Body anpassen | |||
* Antwort inspizieren: Statuscode, Header, Body | |||
'''Ablauf''': | |||
# Öffnen → Neue Anfrage | |||
# Methode + URL eingeben (z. B. https://localhost:7070/api/products) | |||
# Optional: Header / Body hinzufügen | |||
# "Send" klicken → Antwort wird unten angezeigt | |||
* | == Zusammenfassung == | ||
* Antwort | * HTTP = Grundlage der Webkommunikation | ||
* | * Zustandslos, Anfrage-Antwort-Modell | ||
* Statuscodes geben Antwortstatus an | |||
* ASP.NET Core erlaubt präzise Steuerung von Anfragen & Antworten | |||
* Tools wie Postman helfen beim Testen von APIs | |||
Version vom 23. Juni 2025, 19:38 Uhr
HTTP-Protokoll (Hypertext Transfer Protocol)
Überblick
- HTTP ist ein zustandsloses Protokoll zur Übertragung von Hypertext (z. B. HTML) im Internet.
- Client-Server-Modell: Der Client (z. B. Browser) sendet Anfragen, der Server antwortet.
- Jede Anfrage ist unabhängig von vorherigen (stateless).
Anfragemodell
- Client Request: Der Client sendet eine HTTP-Anfrage.
- Server Response: Der Server verarbeitet die Anfrage und antwortet mit Ressourcen oder Fehlermeldungen.
HTTP-Server
Definition: Software, die HTTP-Anfragen verarbeitet und Antworten liefert.
Beispiele:
- Apache HTTP Server
- Nginx
- Microsoft IIS
- Kestrel (ASP.NET Core)
Kestrel:
- Cross-Platform Webserver in ASP.NET Core
- Leichtgewichtig und leistungsfähig
Ablauf von Anfrage und Antwort mit Kestrel
- Client sendet Anfrage
- Kestrel empfängt und leitet sie an Middleware weiter
- Middleware verarbeitet Anfrage
- Anwendung erzeugt Antwort
- Kestrel sendet Antwort zurück an den Client
Browser & HTTP
- Browser fordern HTML, CSS, JS, Bilder über HTTP an
- Klick auf Link → HTTP-Request → Server → Response → Darstellung im Browser
HTTP-Anfragen in Chrome Dev Tools beobachten
- Öffnen: F12 oder Ctrl+Shift+I (Cmd+Option+I auf Mac)
- Reiter „Network“ → Details zu:
- Headers
- Response
- Timing
Aufbau einer HTTP-Antwort
Format:
HTTP/1.1 200 OK Content-Type: text/html Content-Length: 137
<html> <body>
Hello, World!
</body> </html>
Wichtige Header:
- Content-Type: Typ der Ressource (z. B. text/html)
- Content-Length: Größe der Antwort
- Server: Infos zum Server
- Set-Cookie: Setzt Cookies im Client
- Cache-Control: Caching-Strategie
Kestrel Standard-Header
- Content-Type: z. B. text/html, application/json
- Server: „Kestrel“
- Date: Zeitstempel der Antwort
HTTP-Statuscodes
Kategorien
- 1xx: Information
- 2xx: Erfolg
- 3xx: Weiterleitung
- 4xx: Clientfehler
- 5xx: Serverfehler
Gängige Codes
- 200 OK
- 201 Created
- 204 No Content
- 400 Bad Request
- 401 Unauthorized
- 403 Forbidden
- 404 Not Found
- 500 Internal Server Error
- 503 Service Unavailable
ASP.NET Core Codebeispiele
Beispiel 1: Antwort-Header setzen
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Run(async (HttpContext context) =>
{
context.Response.Headers["MyKey"] = "my value";
context.Response.Headers["Server"] = "My server";
context.Response.Headers["Content-Type"] = "text/html";
await context.Response.WriteAsync("<h1>Hello</h1>");
await context.Response.WriteAsync("<h2>World</h2>");
});
app.Run();
Beispiel 2: Statuscode setzen
app.Run(async (HttpContext context) =>
{
context.Response.StatusCode = 200;
await context.Response.WriteAsync("Hello");
await context.Response.WriteAsync(" World");
});
HTTP-Request-Aufbau
- Startzeile: Methode, URI, HTTP-Version
- Header: z. B. User-Agent, Accept, Authorization
- Leerzeile
- Body (optional): z. B. JSON, Formulardaten
Beispiel: GET-Anfrage
GET /products?category=electronics&brand=apple HTTP/1.1 Host: example.com
Beispiel: POST-Anfrage
POST /login HTTP/1.1 Host: example.com Content-Type: application/x-www-form-urlencoded
username=john&password=secret
Query Strings
- Parameter in URL übergeben
- Beispiel:
https://example.com/products?category=electronics&brand=apple
Zugriff auf Request-Details in ASP.NET Core
Beispiel: Pfad und Methode auslesen
app.Run(async (HttpContext context) =>
{
string path = context.Request.Path;
string method = context.Request.Method;
context.Response.Headers["Content-type"] = "text/html";
await context.Response.WriteAsync($"<p>{path}</p>");
await context.Response.WriteAsync($"<p>{method}</p>");
});
Beispiel: Query-Parameter auslesen
app.Run(async (HttpContext context) =>
{
if (context.Request.Method == "GET" &&
context.Request.Query.ContainsKey("id"))
{
string id = context.Request.Query["id"];
await context.Response.WriteAsync($"<p>ID: {id}</p>");
}
});
Beispiel: User-Agent anzeigen
app.Run(async (HttpContext context) =>
{
if (context.Request.Headers.ContainsKey("User-Agent"))
{
string agent = context.Request.Headers["User-Agent"];
await context.Response.WriteAsync($"<p>{agent}</p>");
}
});
HTTP-Methoden
- GET: Daten abfragen, sichtbar in URL, cachebar, sicher
- POST: Daten senden (z. B. Formulare), im Body, nicht idempotent
GET vs POST
GET:
- Daten in URL
- cachebar, idempotent
- nicht für sensible Daten
POST:
- Daten im Body
- sicherer für Passwörter
- nicht idempotent
Tool: Postman
Verwendung:
- HTTP-Requests (GET, POST, usw.) erstellen
- Header & Body anpassen
- Antwort inspizieren: Statuscode, Header, Body
Ablauf:
- Öffnen → Neue Anfrage
- Methode + URL eingeben (z. B. https://localhost:7070/api/products)
- Optional: Header / Body hinzufügen
- "Send" klicken → Antwort wird unten angezeigt
Zusammenfassung
- HTTP = Grundlage der Webkommunikation
- Zustandslos, Anfrage-Antwort-Modell
- Statuscodes geben Antwortstatus an
- ASP.NET Core erlaubt präzise Steuerung von Anfragen & Antworten
- Tools wie Postman helfen beim Testen von APIs